更改类名称空间后无法反序列化

更改类名称空间后无法反序列化

本文介绍了更改类名称空间后无法反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有Seri​​alizable属性的类,并且在该类内是另一个被引用的类,该类也具有Seri​​alizable属性.现在,每当我尝试反序列化使用原始设计序列化的对象时,我都将引用的类移动到另一个名称空间,但抛出异常:System.Rutime.Serialization.TypeLoadExceptionHolder无法转换为类型"MyObjNamespace.MyObj".

这是我为解决此问题而编写的代码示例,但无法正常工作.有人可以告诉我我在做什么错吗?提前谢谢!

I have a class that has the Serializable attribute and within this class is another class that is referenced which also has the Serializable attribute. I moved the class that is reference into another namespace now whenever I try to deserialize objects that were serialized with the original design I throws the the exception: System.Rutime.Serialization.TypeLoadExceptionHolder cannot be converted to type "MyObjNamespace.MyObj".

Here''s an example of the code I''ve written to try to fix this but it''s not working. Can someone please tell what am I doing wrong here? Thanks in advance!

public class SerializationNamespaceConverter : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        typeName = typeName.Replace("oldNamespace, "newNamespace");
        return Type.GetType(typeName);
    }
}

BinaryFormatter biFrmtr = new BinaryFomatter();
biFrmtr.Bind = new SerializationNamespaceConverter();

List<MyObj> myObjs = (List<MyObj>) biFrmtr.Deserialize(myStream);

推荐答案


这篇关于更改类名称空间后无法反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:58