【问题标题】:Serialize a List of my class to Windows Phone Isolated Storage将我的班级列表序列化到 Windows Phone 隔离存储
【发布时间】:2012-05-06 08:29:58
【问题描述】:

我正在使用 .net XmlSerializer 类将我的游戏状态序列化到独立存储中。
这让我可以避免用大量属性弄乱我的代码。

每当我尝试序列化我的公共数据结构列表时都会遇到异常:
“在序列化 GameState_test.Planet 类型的对象时检测到循环引用”

我该如何解决这个问题?我已经研究了很多答案,但没有一个与 WP7 相关。

public class Hazard { public Planet CurrentPlanet;} //reference to the planet its on

public struct Inventory
{
    public Inventory(int coins = 0, int arrows = 0) { Coins = coins; Arrows = arrows; }

    public int Coins;
    public int Arrows;
}

public class Planet
    {
        public Inventory Inventory;
        internal readonly int Index;              
        internal readonly List<int> Connections;  
        public Hazard pHazard; //hazard currently on planet
    }

【问题讨论】:

  • 什么异常,确切等? (同样,但次要:支持类的预期目的(DTO 的序列化)的属性如何“弄乱”代码?)
  • 这是一个 invalidOperationException,我没有一个单独的类,其唯一目的是序列化。
  • 请发布整个异常消息,包括内部异常。
  • @Griffin 序列化不是唯一目的并不重要:您正在使用它进行序列化:这足以使序列化属性合理。
  • 确实;此处的循环通常意味着父/子 - XmlIgnore 通常可以解决此问题,但我们需要看一个示例。其他序列化器也可能是一个选项(例如,protobuf-net 可以支持引用)

标签: c# xml windows-phone-7 serialization xml-serialization


【解决方案1】:

您使用的序列化程序不支持序列化循环引用。切换到 DataContract 序列化程序 (System.Runtime.Serialization) 并遵循本指南:http://blogs.msdn.com/b/sowmy/archive/2006/03/26/561188.aspx
或使用支持它的第三方序列化程序。

【讨论】:

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