在XNA 4.0中序列化字典

时间:2011-07-17 16:25:47

标签: c# serialization dictionary xna-4.0 idictionary

我想在XNA 4.0中序列化一个树,其中每个节点都有一个由int索引的成员字典中的子节点:

[Serializable]
public class Node
{
    private Dictionary<int, Node> children;
}

我的意图是,当我序列化特定节点时,所有以该节点为根的子树都会被序列化。 但是当我尝试测试它时,它似乎在序列化字典时出现问题,它回复了错误(简化):

    System.InvalidOperationException was unhandled
  Message=There was an error reflecting type 'Baddies.Node.Node'.
  InnerException: System.NotSupportedException
       Message=Cannot serialize member Baddies.Nodes.Node.Children of type System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Baddies.Nodes.Node, Baddies, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], because it implements IDictionary.

我的问题是双重问题。首先,如果类Dictionary是可序列化的,那么这会做我期望它做的吗? (即序列化所有子树)。其次,我如何进行字典类的序列化呢?

欢迎任何和所有信息。谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

看来实现IDictionary的类型无法使用XmlSerializer直接序列化。

请阅读此处了解如何解决此问题:XML Serialize IDictionary types (Hashtable, DictionaryBase etc)