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));}}
posted @ 2013-09-11 21:20  遥望星空  阅读(437)  评论(0编辑  收藏  举报