为什么不XmlSerializer的支持字典? [英] Why doesn't XmlSerializer support Dictionary?

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

问题描述

只是好奇,为什么词典不是由的XmlSerializer 支持?

Just curious as to why Dictionary is not supported by XmlSerializer?

您可以通过避开它很轻松了的DataContractSerializer 和写作的对象为的XmlTextWriter ,但什么是一个字典,使得它很难让的XmlSerializer 来对付考虑它真的KeyValuePairs的数组。

You can get around it easily enough by using DataContractSerializer and writing the object to a XmlTextWriter, but what are the characteristics of a Dictionary that makes it difficult for a XmlSerializer to deal with considering it's really an array of KeyValuePairs.

在事实上,你可以传递一个的IDictionary< TKEY的,TItem> 来的方法期待一个的IEnumerable< KeyValuePairs< TKEY的,项目>>

In fact, you can pass an IDictionary<TKey, TItem> to a method expecting an IEnumerable<KeyValuePairs<TKey, ITem>>.

推荐答案

Hashtable中需要哈希code和相等比较器提供商通常。这些不能很容易地在XML序列化,而绝对不会移植。

Hashtables need hashcode and equality comparer providers generally. These cant be serialized easily in XML, and definitely will not be portable.

但我认为你已经找到了答案。只是序列化哈希表为一个列表&LT; KeyValuePair&LT; K,V&GT;&GT; 然后(重新)构建成一个哈希表

But I think you already found your answer. Just serialize the hashtable as a List<KeyValuePair<K,V>> and then (re)construct it into a hashtable.

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

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