Framework / Serialization / CLR Serialization
In This Topic
    CLR Serialization
    In This Topic

    NOV implements its own CLR serialization which allows you to save / load the state of any .NET object graph in XML or Binary format to a file or stream. The following sections discuss how to work with this serialization.

     Saving Object State
    The following code snippet shows how to save the state of "someObject" to a stream in binary format:
    Saving Object State to a Stream
    Copy Code
    NSerializer serializer = new NSerializer();
    serializer.SaveToStream(someObject, stream, ENPersistencyFormat.Binary);
    

    Similarly saving an object state to a file is achieved with the following code:

    Saving Object State to a File
    Copy Code
    NSerializer serializer = new NSerializer();
    serializer.SaveToFile(someObject, "c:\\someFile.bin", ENPersistencyFormat.Binary);
    
     Loading Object State

    In order to load an object which was previously persisted in a stream or file you must use the NDeserializer:

    Loading Object State From a Stream
    Copy Code
    NDeserializer deserializer = new NDeserializer();
    object someObject = deserializer.LoadFromStream(stream, ENPersistencyFormat.Binary);
    
    Loading Object State From a File
    Copy Code
    NDeserializer deserializer = new NDeserializer();
    object someObject = deserializer.LoadFromFile("c:\\someFile.bin", ENPersistencyFormat.Binary);
    

     Excluding Fields From Serialization

    By default all class and struct fields are serializable. If you want to exclude certain fields from serialization you must mark them with the non serializable attribute. For example consider the following class:

    Excluding Fields from Serialization
    Copy Code
    public class MyObject
    {
    public MyObject()
    {
    
    }
    
    public bool FieldA;
    
    [NNonSerialized]
    public bool FieldB;
    
    public bool FieldC;
    }
    

    Note that "FieldB" is marked as non serialized. If we save and load this object FieldB will always be set to false (its default value), regardless of its value when the object was persisted:

    Excluding Fields from Serialization
    Copy Code
    // Save the object
    MyObject myObject = new MyObject();
    
    myObject.FieldA = true;
    myObject.FieldB = true;
    myObject.FieldC = true;
    
    NSerializer serializer = new NSerializer();
    
    serializer.SaveToStream(myObject, stream, ENPersistencyFormat.Binary);
    
    
    // Load the object
    NDeserializer deserializer = new NDeserializer();
    
    stream.Seek(0, SeekOrigin.Begin);
    MyObject myObject = (MyObject)deserializer.LoadFromStream(stream, ENPersistencyFormat.Binary);
    // FieldA is true, FieldB is false, FieldC is true
    

     

     Custom Serializable Objects

    Implementing custom serializable objects is done by declaring two static methods for the class - Serialize and Deserialize. The following sample shows how the class MyObject is extended to use custom serialization:

    Implementing Custom Serialization
    Copy Code
    public class MyObject
    {
    public MyObject()
    {
    
    }
    
    public bool FieldA;
    public bool FieldB;
    public bool FieldC;
    
    public static void Serialize(NSerializationContext context, MyObject myObject)
    {
    // save object fields
    context.WriteBoolean(myObject.FieldA);
    context.WriteBoolean(myObject.FieldC);
    }
    
    public static void Deserialize(NDeserializationContext context, MyObject myObject)
    {
    // load object fields
    myObject.FieldA = context.ReadBoolean();
    myObject.FieldC = context.ReadBoolean();
    }
    }
    
    Note that the code above does not write the value of FieldB so the effect of the serialization / deserialization of MyObject is the original value of FieldB will be lost.
     Surrogate Serializable Objects

    Some objects can have only a limited number of instances. Such objects are called singletons as discussed in the DOM serialization topic. In order to serialize / deserialize such objects using the CLR serialization you need to declare a static method called GetSurrogateSerializer. For example consider the following singleton / surrogate serializer pair:

    Saving Object State to a Stream
    Copy Code
    public class NMySingleton
    {
    private NMySingleton()
    {
    }
    
    public static object GetSurrogateSerializer(NMySingleton singleton)
    {
    return new NMySurrogate(singleton);
    }
    
    public static NMySingleton InstanceA = new NMySingleton();
    public static NMySingleton InstanceB = new NMySingleton();
    }
    
    public class NMySurrogate : INSurrogateSerializer 
    {
    public NMySurrogate()
    {
    
    }
    
    public NMySurrogate(NMySingleton instance)
    {
    IsInstanceA = instance == NMySingleton.InstanceA;
    }
    
    public object GetRealObject()
    {
    if (IsInstanceA)
    {
    return NMySingleton.InstanceA;
    }
    else
    {
    return NMySingleton.InstanceB;
    }
    }
    
    public void ApplyToRealObject(object obj)
    {
    // not implemented
    }
    
    bool IsInstanceA;
    }
    

     

    See Also