不同程序集中T列表的反序列化 [英] Deserialization of List of T in different assembly

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

问题描述

我有一个使用BinaryFormater序列化类的应用程序,我将调用类A,它包含一个包含另一个类的T列表的单个属性,我将调用类B.我正在使用第二个应用程序反序列化这个类。谷歌搜索找到了MSDN和其他如何使用BinaryFormater.Binder来解除在不同网络中序列化的对象的示例。当我执行我的代码以反序列化第二个程序集中的类A时,我看到的是调用的Serialization Binder被调用并返回我当前程序集中的A类的类型,我希望按预期反序列化,然后再次调用相同的Binder,这次作为参数传递给类B的类型名称和汇编来自序列化类B和序列化B的程序集。但我的序列化Binder只为A类设置,而不是B类对象在它的List属性中,抛出异常。如何反序列化包含不同组件中的List或其他对象集合的Object?

I have one application that is using a BinaryFormater to serialize a class, I will call class A, that contains a single property with is a List of T containing another class, I will call class B. I am using a second application to deserialize this class. A Google search found the MSDN and other examples of how to use BinaryFormater.Binder to deserailize an object that was serialized in a differnet assmebly. When I execute my code to deserialize class A in the second assembly, what I am seeing is that the Serialization Binder called is called and returns the Type for the for class A that is in my current assembly that I wish to deserialize to as expected, then the same Binder is called again, this time being passed as arguments the Type Name and Assembly for class B from the orginal Type and Assembly that serialized class B. But my Serialization Binder is only set up for class A, not the class B object that are in it''s List property and an exception is thrown. How do you deserialize a Object that contains a List or collection of other Objects in a differnet assembly?

推荐答案

根据我在您的问题中的理解,您实际上是序列化和de - 查看看起来相同的不同类,但CLR将它们视为不同,因为它们是在独立程序集中声明的。



解决这个问题的常用方法是创建一个新程序集,包含所有序列化类和可能的序列化/反序列化代码。然后让两个程序集引用这个新程序集。现在您应该能够在两个程序集中使用序列化/反序列化,因为它们指的是同一个类,即共享的新程序集中的类。
From what I understand in your question you are actually serializing and de-serializing different classes that are looking the same, but CLR is treating them as different since they are declared in independent assemblies.

A common way to solve this is creating a new assembly that contains all the serialized classes and possible the serialization/de-serialization code. Then make both your assemblies reference this new assembly. Now you should be able to use the serialization / de-serialization in both assemblies, because they are referring to the same class, the one in the shared, new assembly.


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

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