序列化两个bindingList [英] Serialization of two bindingList

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

问题描述

我创建了一个winForm,该窗体具有两个bindingList,用于保存两种不同类型的对象.我想将两者序列化为我可以执行的相同流.接下来,我希望能够反序列化流,但是能够反序列化每个bindList并将它们分配给新的bindingList.示例:

I''ve created a winForm that has two bindingList that hold two different types of objects. I''d like to serialize both to the same stream which I''m able to do. Next I''d like to be able to deserialize the stream but be able to deserialize each bindList and assign them to new bindingList. Example:

BindingList<myobj1> obj1 = new BindingList<myobj1>() {new MyObj1(), new MyObj1()};
BindingList<myobj2> obj2 = new BindingList<myobj2>() {new MyObj2(), new MyObj2()};

BinaryFormatter serializer = new BinaryFormatter();
Stream strm = File.Create("C:\MyObjDir");
serializer.Serialize(strm, obj1);
serializer.Serialize(strm, obj2);

// Now I want to be able to somehow deserialize them. I know this doesn't work
// but this is along the lines of what I'm trying to accomplish

Stream strm = File.Open("C:\MyObjDir");
BindingList<myobj1> obj1 = (BindingList<myobj1>) serializer.Deserialize(strm);
BindingList<myobj2> obj2 = (BindingList<myobj2>) serializer.Deserialize(strm);

推荐答案

您可以使用数据联系.请参阅:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

您可能会遇到数据协定完美解决的问题:同一对象在数据中的引用可能超过一次,因此它可能导致序列化中的无限递归.使用数据协定序列化程序,可以在序列化程序的构造函数中使用System.Runtime.Serialization.DataContractAttribute的属性IsReference或参数preserveObjectReferences来保留对象引用.它将允许您序列化不是树的对象图(即可以包含循环引用).请参阅:
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractattribute.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractserializer.aspx [ ^ ].

(我没有在System.Runtime.Serialization.Json.DataContractJsonSerializer中看到此功能,因此您必须使用XML格式的序列化.)

在我倡导数据合同方法时,也请参阅我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [
You can use Data Contact. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

You may face the problem the Data Contract addresses perfectly: the same object can be referenced in the data more then once, so it can cause infinite recursion in serialization. With Data Contract serializers, you can use the property IsReference of the System.Runtime.Serialization.DataContractAttribute or the parameter preserveObjectReferences in the constructors of the serializers to preserve the object references. It will allow you to serialize an object graph which is not a tree (that is, can contain circular references). Please see:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^].

(I don''t see this feature in System.Runtime.Serialization.Json.DataContractJsonSerializer, so you have to use XML-formatted serialization.)

Please see also my past answers where I advocate the Data Contract approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


更多快速而肮脏"的解决方案,您可以尝试制作BindingList< Object>来自其他两个BindingList.
现在,当您对它进行反序列化时,只需执行以下操作即可:
A bit more ''quick and dirty'' solution you could try making a BindingList<Object> from the other two BindingLists.
Now when you deserialize it just do something like the following:
BindingList<object> objBl = (BindingList<object>)serializer.Deserialize(strm);
BindingList<myObj1> bl1 = new BindingList<myObj1>(objBl.OfType<myObj1>().ToList());
BindingList<myObj2> bl2 = new BindingList<myObj2>(objBl.OfType<myObj2>().ToList());

应该可以,但正如我所说的那样" s快速又肮脏的解决方案.
我会选择SAKryukov的解决方案,但由于我知道这不是最简单的解决方案(但到目前为止是最好的),因此我想为您提供一个更简单的解决方案.
请注意,这可能会在短期内为您提供帮助,但稍后会回来并咬您:)

Should work, but as I said it''s the quick and dirty solution.
I''d take SAKryukov''s solution, but since I understand that''s not the easiest solution (but best by far) I wanted to give you an easier solution.
Beware though, this might help you out in the short run, but it will come back and bite you later on :)


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

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