BinaryFormatter反序列化 [英] BinaryFormatter Deserialization

查看:77
本文介绍了BinaryFormatter反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在使用BinaryFormatter将对象序列化为文件。


当我反序列化对象时从文件中,我重写 BindToType
方法将对象绑定到正确的类型。


当序列化对象时,文件中存储了多个对象。我可以看到文件中有对象以及程序集名称及其版本信息。


允许将此文件称为xyz.txt [包含序列化对象]


现在,我在程序集中进行了一些更改。所以我的问题是,对这个文件xyz.txt的反序列化操作会成功吗? 


当我在反序列化时通过方法 BindToType 进行调试时,看起来似乎没问题其中一个对象给我铸造类型错误。而且我无法针对此播放问题进一步调试,因为我不知道这个
BindToType 方法返回结果的位置。


这是我的反序列化类被重写了BindToType方法:

 sealed class DeserializationBinder:SerializationBinder 
{
public override Type BindToType(string assemblyName,string typeName)
{
try
{

类型t = Type.GetType(String.Format(" {0},{1}",
typeName ,assemblyName));
if(t!= null)
{
return t;
}
errorTypes.Add(typeName);
返回typeof(QAliber.TestModel.TestCases.LoadErrorTestCase);
}
catch(例外e)
{
返回typeof(QAliber.TestModel.TestCases.LoadErrorTestCase);
}
}

内部静态列表< string> errorTypes = new List< string>();
internal static int errorIndex = 0;

}




 

所以我不确定,是否是版本控制问题[因为我更改了程序集的版本]


在这种情况下,我很空白。


你可以指导我吗?


提前谢谢你!


Navnath



 

解决方案

Hello Navnathk,


对于强名称的程序集,其程序集名称,版本,文化,publicKeytoken共同标识程序集。例如。

 MyTypes,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 111111111111111 
MyTypes,Version = 1.0.0.0,Culture =" en-us",PublicKeyToken = 111111111111111
MyTypes,Version = 1.0.0.2,Culture = neutral,PublicKeyToken = 111111111111111
MyTypes,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 222222222222222

尽管程序集名称相同,但上述程序集完全不同。根据您的情况,如果更改程序集的版本,则dll中的对象和序列化文件中的对象是不同的类型。在反序列化时,它会导致转换
问题。


有关汇编的更多详细信息,您可以看到
here。
 


https://msdn.microsoft。 com / zh-CN / library / wd40t7ad(VS.71).aspx


祝你好运,


Neil Hu


Hello,

I am using BinaryFormatter for serializing object(s) into a file.

And when I deserialize the object from file, I am overriding BindToType method to bind the object to correct type.

There are number of objects stored in file when I serialized objects. I can see that objects are there in file along with assembly name and its version information.

Lets this file called as xyz.txt [which contains serialized objects]

Now, I made some changes in a assembly. So my question is, will deserialize operation on this file xyz.txt work successfully? 

When I debug through method BindToType while deserializing, it seems OK until one of the object giving me casting type error. And I am not able to debug further for this casting issue because I don't know where this BindToType method returns it result.

Here is my Deserialization class with BindToType method overridden:

  sealed class DeserializationBinder : SerializationBinder
    {
        public override Type BindToType(string assemblyName, string typeName)
        {
            try
            {
                
                Type t = Type.GetType(String.Format("{0}, {1}",
                typeName, assemblyName));
                if (t != null)
                {
                    return t;
                }
                errorTypes.Add(typeName);
                return typeof(QAliber.TestModel.TestCases.LoadErrorTestCase);
            }
            catch(Exception e)
            {
                return typeof(QAliber.TestModel.TestCases.LoadErrorTestCase);
            }
        }

        internal static List<string> errorTypes = new List<string>();
        internal static int errorIndex = 0;

    }


 

So I am not sure, whether it is issue of versioning [since I changed version of an assembly]

I am quite blank in this case.

Could you please guide me for this?

Thank you in advance!

Navnath

 

解决方案

Hello Navnathk,

For strong-named Assemblies, whose assembly name, version ,culture, publicKeytoken collectively identify the assembly. For example.

MyTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=111111111111111
MyTypes, Version=1.0.0.0, Culture="en-us", PublicKeyToken=111111111111111
MyTypes, Version=1.0.0.2, Culture=neutral, PublicKeyToken=111111111111111
MyTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=222222222222222

The above assemblies are completely different things although the assemblies name are same. As for your circumstance, If you change the version of assembly, the object from the dll and the object from serialize file is different type. it will cause the casting issue when you deserialize.

For more detailed info about assembly, you could see here. 

https://msdn.microsoft.com/en-us/library/wd40t7ad(VS.71).aspx

Best regards,

Neil Hu


这篇关于BinaryFormatter反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆