Search
BinarySerializer Interface
Remarks See Also
 





Defines an interface for custom serialization of Tag objects.

Namespace: MindFusion.Diagramming
Assembly: MindFusion.Diagramming

 Syntax

C#  Copy Code

public interface BinarySerializer

Visual Basic  Copy Code

Public Interface BinarySerializer

 Remarks

BinarySerializer was introduced to provide an alternative to the now-deprecated .NET BinaryFormatter. While PersistContext automatically handles primitive types and enums using System.IO.BinaryWriter operations, it historically fell back to BinaryFormatter for complex objects stored in Tag properties and for ControlNode serialization. Implementing this interface allows you to provide serialization logic for your custom types. Assign an implementing object to the BinarySerializer property of PersistContext, and the interface methods will be called while saving to / loading from binary format.

 Example

You can serialize a struct set as a Tag value like this:

C#  Copy Code

public struct MyCustomData
{
    public int ID;
    public string Description;

    public MyCustomData(int id, string desc)
    {
        ID = id;
        Description = desc;
    }
}

public class MyDiagramSerializer : BinarySerializer
{
    // return true if this serializer handles the object type
    public bool ShouldSerialize(string propertyName, object propertyValue, object owner)
    {
        return propertyValue is MyCustomData;
    }

    // write the struct fields to the binary stream
    public void Serialize(BinaryWriter writer, string propertyName, object propertyValue, object owner)
    {
        if (propertyValue is MyCustomData data)
        {
            // consider writing struct name and versioning info here
            writer.Write(data.ID);
            writer.Write(data.Description ?? string.Empty);
        }
    }

    // reconstruct the struct from the binary stream
    public object Deserialize(BinaryReader reader, string propertyName, object owner)
    {
        // consider reading struct name and versioning info here
        int id = reader.ReadInt32();
        string desc = reader.ReadString();
        return new MyCustomData(id, desc);
    }
}

// register your custom serializer globally
PersistContext.BinarySerializer = new MyDiagramSerializer();

// create a node and set its Tag to your custom struct
ShapeNode node = diagram.Factory.CreateShapeNode(10, 10, 40, 30);
node.Tag = new MyCustomData(42, "Sample custom tag data");

// now, when you call diagram.SaveToFile(), your
// MyDiagramSerializer will be used for the Tag property.
diagram.SaveToFile("my_diagram.bin");

 See Also