大会独立的序列化在.NET中 [英] Assembly Independent Serialization in .NET

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

问题描述

我使用的序列化/反序列化技术。 BinaryFormatter类。
每次创建新的装配时的BinaryFormatter无法反序列化,即使该类结构是相同的二进制数据,但大会的版本不同。
是否有可能反序列化二进制缓冲区,没有检查的程序集版本,如果类的结构保持不变?

I use Serialization/DeSerialization Technique. BinaryFormatter class. Each time when new assembly is created the BinaryFormatter can't Deserialize binary data even if the class structure is the same, but the Assembly version differs. Is it possible to Deserialize binary buffer with no checking the assembly version if the class structure stays unchanged?

推荐答案

尝试这样的:

public sealed class CurrentAssemblyDeserializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        return Type.GetType(String.Format("{0}, {1}", typeName, Assembly.GetExecutingAssembly().FullName));
    }
}
formatter.Binder = new CurrentAssemblyDeserializationBinder();
formatter.Deserialize(inStream);



主题海报加:

是的,它的工作原理。只要确保如果二进制数据的任何类型的System.Generic或其他利布斯都存在,那么你必须通过通过他们无需改动。
ResizableControls - 老大会库'的名字,EntityLib - 新议会名称。
此外,版本号为被替换为好,按需

Yes, it works. Just make sure if any types of System.Generic or other Libs in the binary data are present, then you have to pass them through without changes. "ResizableControls" - old Assembly lib' name, "EntityLib" - new Assembly name. Also, the Version number is to be replaced as well, on demand.

public sealed class CurrentAssemblyDeserializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        string name;
        if (assemblyName.Contains("ResizableControl"))
        {
            name = Assembly.GetAssembly(typeof(EntityLib.Pattern)).ToString();
        }
        else
        {
            name = assemblyName;
        }
        return Type.GetType(String.Format("{0}, {1}",
            typeName.Replace("ResizableControl", "EntityLib"), name));
    }
}



谢谢,这正是我所需要的。

Thanks, it is exactly what I needed.

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

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