为什么不能在C#中的字典对象中XmlSerialized? [英] Why can't a dictionary object be XmlSerialized in C#?

查看:209
本文介绍了为什么不能在C#中的字典对象中XmlSerialized?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,序列化是非常简单的。假设这两个键和值是序列化的,有什么能比重新presenting键 - 值对的XML简单?!

It seems that serialization is very straightforward. Assuming that both the key and value are serializable, what could be simpler than representing key-value pairs in XML?!

要所有的评论者:首先,我AP preciate你的答案,但是 - 我在workoraunds不感兴趣(的确有很多SerializableDictionary实现在网络上采摘),更是为原因这个的的设计决策的。

To all the commenters: First of all, I appreciate your answers, But- I am less interested in workoraunds (There is indeed plenty of SerializableDictionary implementations on the web for picking) and more as to the reason for this design decision.

推荐答案

为什么的一部分:

不幸的是有没有办法让XmlSerializer的功能与IDictionary的派生的对象,因为基础设施的明确检查的IDictionary在运行时禁用系列化。解决此问题的方法是写一个包装一个IDictionary对象并拷贝值到序列化对象的数组一个新的类。 XmlSerializer的框架还有一个隐藏的钩子编写自定义序列化code。要利用此挂钩可以实现所谓的IXmlSerializable为要序列化/反序列化对象的接口优势。

Unfortunately there's no way to make XmlSerializer function with IDictionary-derived objects since the infrastructure explicitly checks for IDictionary at run time and disables serialization. One way around this is to write a new class that wraps an IDictionary object and copies the values into an array of serializable objects. The XmlSerializer framework also has a hidden hook for writing custom serialization code. To take advantage of this hook you can implement an interface called IXmlSerializable for the object you want to serialize/deserialize.

当你实例化一个XmlSerializer对象,构造器通常会产生含有的XmlReader和XmlWriter的code的对象实例和XML文档之间移动的临时组装。在此之前,但是,它首先检查是否所提供的类型从派生的IXmlSerializable,如果是这样,它生成code调用到的IXmlSerializable成员代替。换句话说,如果你实现IXmlSerializable的,你完全跳过自动序列化过程,并有机会提供你自己的。

When you instantiate an XmlSerializer object, the constructor usually generates a temporary assembly containing XmlReader and XmlWriter code for moving between object instances and XML documents. Before doing this, however, it first checks to see if the supplied type derives from IXmlSerializable and if so, it generates code to call into the IXmlSerializable members instead. In other words, if you implement IXmlSerializable, you completely bypass the automatic serialization process and have the opportunity to provide your own.

您可以在这里阅读整篇文章: XML文件:高级类型映射

You can read entire article here: XML Files: Advanced Type Mappings

这篇关于为什么不能在C#中的字典对象中XmlSerialized?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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