【问题标题】:Serializing dictionary in XNA 4.0在 XNA 4.0 中序列化字典
【发布时间】:2011-10-07 04:47:57
【问题描述】:

我想在 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 是可序列化的,这会像我期望的那样吗? (即序列化所有子树)。其次,那我该如何序列化字典类呢?

欢迎提供任何和所有信息。感谢您的宝贵时间。

【问题讨论】:

    标签: c# serialization dictionary xna-4.0 idictionary


    【解决方案1】:

    似乎实现 IDictionary 的类型无法使用 XmlSerializer 开箱即用地序列化。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      相关资源
      最近更新 更多