You can also use the SerializationBinder to determine which type will be loaded if another type is deserialized:
public sealed class Version1ToVersion2DeserializationBinder : SerializationBinder { public override Type BindToType(string assemblyName, string typeName) { Type typeToDeserialize = null; if (typeName == "OldClassName") typeName = "NewClassName"; typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName)); return typeToDeserialize; } }
To deserialize, you just need to set the Binder property for the BinaryFormatter :
formatter.Binder = new Version1ToVersion2DeserializationBinder(); NewClassName obj = (NewClassName)formatter.Deserialize(fs);
source share