参见英文答案 >
duplicated id with fragment3个
> Is it possible to recover an object serialized via “BinaryFormatter” after changing class names?1个
更改我的类的命名空间后,我不能再反序列化对象.我已经实现了SerializationBinder.例:
> Is it possible to recover an object serialized via “BinaryFormatter” after changing class names?1个
更改我的类的命名空间后,我不能再反序列化对象.我已经实现了SerializationBinder.例:
public class TypeNameConverter : SerializationBinder { public override Type BindToType(string assemblyName,string typeName) { typeName = typeName.Replace("MyOldNamespace","MyNewNamespace"); return Type.GetType(string.Format("{0},{1}",typeName,assemblyName)); } } BinaryFormatter bf = new BinaryFormatter(); bf.Binder = new TypeNameConverter();
我得到的例外是:
‘System.Runtime.Serialization.TypeLoadExceptionHolder’ cannot be converted to type ‘MyNewNamespace.MyClass’
解决方法
你忘了更换程序集名称:
class TypeNameConverter : SerializationBinder { public override Type BindToType(string assemblyName,"MyNewNamespace"); assemblyName = assemblyName.Replace("MyOldNamespace",assemblyName)); } }