CrossMark
object already familiar to us: [CustomEntityAttribute("1C925FA1-842B-49CD-924F-4ABF9717DB62", 2, "Crossmark", "Crossmark Sample Entity")] [Serializable] public class CrossMark : McCustomBase { private Point3d pnt1; private Point3d pnt2; private Point3d pnt3; private Point3d pnt4; private double radius; }
radius
field remains unchanged: [CustomEntityAttribute("1C925FA1-842B-49CD-924F-4ABF9717DB62", 3, "Crossmark", "Crossmark Sample Entity")] [Serializable] public class CrossMark : McCustomBase { private Vector3d v1; private Vector3d v2; private Vector3d v3; private Vector3d v4; private double radius; }
ISerializable
interface, which allows controlled serialization of objects.ISerializable
, the implementation of two methods is necessary:public void GetObjectData(SerializationInfo info, StreamingContext context)
- used to serialize an object.public CrossMark(SerializationInfo info, StreamingContext ctx)
- the constructor used for deserialization. // Serialization public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("vec1", v1); info.AddValue("vec2", v2); info.AddValue("vec3", v3); info.AddValue("vec4", v4); info.AddValue("radius", radius); }
// Deserialization public CrossMark(SerializationInfo info, StreamingContext ctx) { radius = info.GetDouble("radius"); try { v1 = (Vector3d)info.GetValue("vec1", typeof(Vector3d)); v2 = (Vector3d)info.GetValue("vec2", typeof(Vector3d)); v3 = (Vector3d)info.GetValue("vec3", typeof(Vector3d)); v4 = (Vector3d)info.GetValue("vec4", typeof(Vector3d)); } catch (System.Runtime.Serialization.SerializationException) { Point3d pnt1 = (Point3d)info.GetValue("pnt1", typeof(Point3d)); Point3d pnt2 = (Point3d)info.GetValue("pnt2", typeof(Point3d)); Point3d pnt3 = (Point3d)info.GetValue("pnt3", typeof(Point3d)); Point3d pnt4 = (Point3d)info.GetValue("pnt4", typeof(Point3d)); v1 = pnt1.GetAsVector(); v2 = pnt2.GetAsVector(); v3 = pnt3.GetAsVector(); v4 = pnt4.GetAsVector(); } }
SerializationException
section of the exception processing section, which will be thrown when trying to deserialize nonexistent fields from the previous version. public void GetObjectData(SerializationInfo info, StreamingContext context) { ... info.AddValue("version", 1); }
public CrossMark(SerializationInfo info, StreamingContext ctx) { int version = info.GetInt("version"); switch (version) { case 1: ... case 2: ... ... }
Source: https://habr.com/ru/post/229175/
All Articles