Live Chat Icon For mobile
Live Chat Icon

How can I do versioning using Custom Serialization ?

Platform: WPF| Category: Serialization and Storage

Versioning can be done using serialization as follows.

public class MyClass : ISerializable
   public int Number;
   public int NewField;
   public void GetObjectData(
       SerializationInfo info,StreamingContext context)

   protected MyClass(SerializationInfo info,StreamingContext context)
      Number = info.GetInt32('Number');
      Version storedVersion = SerializationUtil.GetVersion(info);
      if(storedVersion.ToString() == '')
         NewField = info.GetInt32('NewField');
         NewField = 123;//Some default value

   public MyClass()

public static class SerializationUtil
   static public Version GetVersion(SerializationInfo info)
      string assemblyName = info.AssemblyName;
      /* AssemblyName is in the form of 'MyAssembly, Version=,  
                              Culture=neutral,PublicKeyToken=null' */
      char[] separators = {’,’,’=’};
      string[] nameParts = assemblyName.Split(separators);
      return new Version(nameParts[2]);
   //Rest of SerializationUtil

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.