繁体   English   中英

如何序列化包含XML队列的类?

[英]How to serialize a class containing queues for XML?

我有一个名为GamePrototype的类,我想对其进行序列化以供XML使用,其中包含以下成员:

public class GamePrototype
{
    private string mPath;
    private bool[] isHumanPlayer;
    private Queue<CardPrototype> mKickStack;
    private Queue<CardPrototype> mMainDeck;
    private Queue<CardPrototype> mStarterDecks;
    private Queue<CardPrototype> mSuperVillianStack;
    private Queue<CardPrototype> mWeaknessStack;
    private Queue<PlayerHero> mPlayableHeroes;
    public Queue<CardPrototype>[] mStartingLocations;
}

我一直在尝试使用此方法序列化类数据。 “ this”是指上述类的一个实例:

public void XMLShelf(string path)
    {
        XmlSerializer boxPacker = new XmlSerializer(typeof(GamePrototype));
        StreamWriter boxPlacer = new StreamWriter(path);

        boxPacker.Serialize(boxPlacer, this);
        boxPlacer.Close();
    }

我有大多数(如果不是全部)的getter和setter,但是我遇到了两个我不知道如何避免的错误:

Exception:Thrown: "You must implement a default accessor on 
System.Collections.Generic.Queue`1[[Cards.GameBuilding.CardPrototype, Cards, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it inherits from 
ICollection."...

Exception:Thrown: "There was an error reflecting type 
'Cards.GameBuilding.GamePrototype'." (System.InvalidOperationException)

我在其他帖子中已经读到,在当前情况下,此类由于队列而无法序列化。 该信息可能已过时? 我不确定,但是我的问题是:

有没有一种方法可以轻松地将这些队列转移到列表中以进行序列化? 对于这些成员所属的所有类,我都需要这样做吗? 例如,Card原型包含一个队列和一个堆栈。

非常感谢您为我提供的任何帮助。

这取决于您使用的序列化。 使用ISerializable可以正常工作。

[Serializable]
class Program : ISerializable
{
    public Program() { }

    public Queue<int> Queue = new Queue<int>(); 

    private unsafe static void Main(string[] args)
    {
        var pr = new Program();
        pr.Queue.Enqueue(1034);

        using (var stream = File.Create("path.txt"))
        {
            var bformatter = new BinaryFormatter();
            bformatter.Serialize(stream, pr);
        }

        using (var stream = File.Open("path.txt", FileMode.Open))
        {
            var bformatter = new BinaryFormatter();
            pr = (Program)bformatter.Deserialize(stream);
        }

        Console.WriteLine("program " + pr.Queue.Peek()); //prints 1034
        Console.Read();
    }

    public Program(SerializationInfo info, StreamingContext ctxt)
    {
        Queue = (Queue<int>)info.GetValue("queue", typeof(Queue<int>));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("queue", Queue);
    }
}

希望这可以帮助。

您必须为队列实现默认访问器,因为XMLSerializer需要它来枚举项目以及Add方法。

[Serializable]
public class SerializableQueue : Queue<MyObject>
{
    public MyObject this[int index]
    {
        get
        {
            int count = 0;
            foreach (MyObject o in this)
            {
                if (count == index)
                    return o;
                count++;
            }
            return null;
        }
    }

    public void Add(MyObject r)
    {
        Enqueue(r);   
    }
}

这是一个可以序列化为XML的队列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM