Why do I encounter problems when attempting to serialize a custom symbol type?
Why do I encounter problems when attempting to serialize a custom symbol type?
The Essential® Diagram base symbol type implements custom serialization and deserialization behavior through the ISerializable interface. Therefore all custom symbol classes that derive from this type will have to implement both the signature serialization Constructor(SerializationInfo info, StreamingContext context) and the ISerializable.GetObjectData(SerializationInfo information, StreamingContext context) method as well and call the respective base class methods.
During serialization, any serializable members belonging to the custom symbol class should be written to the SerializationInfo param in the GetObjectData() method and upon deserialization should be populated with the equivalent values from the SerializationInfo param provided by the serialization constructor.
The following sample shows the serialization implementation for a custom symbol class:
[C#]
/// /// The MySymbol class implements a custom Essential Diagram Symbol type. /// [ Serializable(), TypeConverter(typeof(MySymbolConverter)) ] public class MySymbol : Symbol { Protected bool bClrFlag = false; /// /// Default constructor. /// public MySymbol() { } /// /// Serialization constructor for the MySymbol class. /// /// Serialization state information /// Streaming context information protected MySymbol(SerializationInfo info, StreamingContext context) : base(info, context) { // The Serialization constructor is invoked during deserialization or during a drag & drop operation. // If the MySymbol type has serializable members, then initialize them with the serialized data // obtained from the SerializationInfo param // Read the bClrFlag member value from the SerializationInfo object this.bClrFlag = info.GetBoolean("ColorFlag"); } // Override SymbolBase.GetObjectData() and populate the SerializationInfo param // with the data (if any) that belongs to the MySymbol type . This data will be // serialized as a part of the Symbol object. protected override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); // Populate the SerializationInfo object with the bClrFlag member data info.AddValue("ColorFlag", this.bClrFlag); } }
[VB.NET]
Public Class MySymbol Inherits Symbol Protected bClrFlag As Boolean '/ '/ Default constructor. '/ Public Sub New() End Sub 'New '/ '/ Serialization constructor for symbols. '/ '/ Serialization state information '/ Streaming context information Protected Sub New(info As SerializationInfo, context As StreamingContext) MyBase.New(info, context) ' The Serialization constructor is invoked during deserialization or during a drag & drop operation. ' If the MySymbol type has serializable members, then initialize them with the serialized data ' obtained from the SerializationInfo param ' Populate the bClrFlag member with the value read from the SerializationInfo object Me.bClrFlag = info.GetBoolean("ColorFlag") End Sub 'New ' Override SymbolBase.GetObjectData() and populate the SerializationInfo param ' with the data (if any) that belongs to the MySymbol type. This data will be ' serialized as a part of the MySymbol object. Protected Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) MyBase.GetObjectData(info, context) ' Populate the SerializationInfo object with the bClrFlag member data info.AddValue("ColorFlag", Me.bClrFlag) End Sub End Class 'MySymbol