I used to serialize a treeview with the BinaryFormatter (c#). The Assembly that did just that and which contains the all the serializable classes has now a strong name and i
You can use a SerializationBinder
to solve this:
private class WeakToStrongNameUpgradeBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
try
{
//Get the name of the assembly, ignoring versions and public keys.
string shortAssemblyName = assemblyName.Split(',')[0];
var assembly = Assembly.Load(shortAssemblyName);
var type = assembly.GetType(typeName);
return type;
}
catch (Exception)
{
//Revert to default binding behaviour.
return null;
}
}
}
Then
var formatter = new BinaryFormatter();
formatter.Binder = new WeakToStrongNameUpgradeBinder();
Voila, your old serialized objects can be deserialized with this formatter. If the type have also changed you can use a SerializationSurrogate
to deserialize the old types into your new types.
As others have mentioned, doing your own serialization rather than relying on IFormatter
is a good idea as you have much more control over versioning and serialized size.