title: Serialization and object versioning in C#
body: <p>If I want to serialize an object I have to use <code>[Serializable]</code> attribute and all member variables will be written to the file. What I don't know how to do versioning e.g. if I add a new member variable (rename a variable or just remove a variable) and then I open (deserialize) the file how can I determine the object/file version so I can correctly set the new member or take some kind of migration? How can I determine that the variable was initialized during the load or not (ignored by deserializer). </p>
  
  <p>I know that there are version tolerant approaches and I can mark variables with <code>[OptionalField(VersionAdded = 1)]</code> attribute. If I open an old file the framework will ignore this optional (new variable) and it will be just zero/null. But again how can I determine if the variable is initialized by load or it was ignored.</p>
  
  <p>I can write the class/object version number to the stream. Use the <code>ISerializable</code> approach and in the <code>constructor(SerializationInfo oInfo, StreamingContext context)</code> method read this version number. This will exactly tell me what is the class version in the stream.</p>
  
  <p>However I expected that such kind of versioning is already implemented by the streaming framework in C#. I tried to obtain the Assembly version from the <code>SerializationInfo</code> but it is always set to current version not to the version which was used when the object was saved.</p>
  
  <p>What is the preferred approach? I found a lot of articles on the net, but I could not find a good solution for this which addresses versioning...</p>
  
  <p>Any help is appreciated 
  Thanks,
  Abyss</p>