Can't deserialize with binaryFormatter after changing namespace of class

After changing the namespace of my class I can no longer deserialize the objects. I've implemented SerializationBinder. Example:

publicclassTypeNameConverter:SerializationBinder{publicoverrideTypeBindToType(string assemblyName,string typeName){ typeName = typeName.Replace("MyOldNamespace","MyNewNamespace");returnType.GetType(string.Format("{0}, {1}", typeName, assemblyName));}}BinaryFormatter bf =newBinaryFormatter(); bf.Binder=newTypeNameConverter();

The exception I get is 'System.Runtime.Serialization.TypeLoadExceptionHolder' cannot be converted to type 'MyNewNamespace.MyClass'

 

you forgot to replace the assembly name:

classTypeNameConverter:SerializationBinder{publicoverrideTypeBindToType(string assemblyName,string typeName){ typeName = typeName.Replace("MyOldNamespace","MyNewNamespace"); assemblyName = assemblyName.Replace("MyOldNamespace","MyNewNamespace");returnType.GetType(string.Format("{0}, {1}", typeName, assemblyName));}}

你可能感兴趣的:(namespace)