Serialization between different date version

  • Thread starter Thread starter Lei Jiang
  • Start date Start date
L

Lei Jiang

I use serialization(binary or SOAP) to save my data. When I change my data
format(add a field to the object for example), a exception will throw that
states that the version of the object might be wrong. Could serialization
support a more flexible manner : If I remove a field from the object, the
field will be ignored when loading data previously saved; If I add a field
to a object, the field will be the default value when loading data
previously saved. It could have a switch to turn this feature on or off.

Could anyone tell me a good way to handle this in this version of .NET? The
format of the object sometimes have to change during development.
 
Here is some code that will help you along. The ISerializable interface is easy enough to work with. The binder is what you need to bind the Type to the data. The default binder considers the assembly name to be relevant. This code doesn't use the assembly name information.

Much nicer when persisting data!!!

This information can be found towards the end of the last article in the previous response.

IFormatter formatter = new BinaryFormatter();
formatter.Binder = new BinderOverride();


private class BinderOverride : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
return Type.GetType(typeName);
}
}
 
Back
Top