如何序列化IDictionary [英] How to serialize IDictionary
本文介绍了如何序列化IDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道一种创造性的方法来序列化实现IDictionary的对象吗?...没有实施新的课程?
Does anyone know of a creative way to serialize objects that implement IDictionary? ...without implementing a new class?
推荐答案
If the class implementing IDictionary
is serializable (like Dictionary<K,V>
) and K
and V
are serializable then the standard .NET serialization mechanisms should work.
如果实现 IDictionary
的类是可序列化的,但是 K
和 V
是,则可以使用两个数组分别序列化键和关联的值:
If the class implementing IDictionary
is serializable but K
and V
are then you could use two arrays to serialize the keys and associated values separately:
// before serialization
IDictionary<string,int> dict;
string[] keys = dict.Keys.ToArray();
int[] values = dict.Keys.Select(key => dict[key]).ToArray();
// after deserialization
IDictionary<string,int> dict = new Dictionary<string,int>();
for (int i = 0; i < keys.Length; i++)
dict.Add(keys[i], values[i]);
这篇关于如何序列化IDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文