简体   繁体   English

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

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

I have a class called GamePrototype that I would like to serialize for XML use, containing the following members: 我有一个名为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;
}

I have been trying to serialize the class data with this method. 我一直在尝试使用此方法序列化类数据。 "this" refers to an instance of the above-mentioned class: “ this”是指上述类的一个实例:

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

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

I have getters and setters for most if not all of them, but I'm getting two errors I don't know how to avoid: 我有大多数(如果不是全部)的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."...

and

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

I've read in other posts that in the current situation, this class is not serializable because of the queues. 我在其他帖子中已经读到,在当前情况下,此类由于队列而无法序列化。 That information might be outdated? 该信息可能已过时? I'm not sure, but my questions are: 我不确定,但是我的问题是:

Is there a way to easily transfer these queues into Lists just for serialization? 有没有一种方法可以轻松地将这些队列转移到列表中以进行序列化? Will I need to do this for all classes these members belong to as well? 对于这些成员所属的所有类,我都需要这样做吗? For example, Card prototype contains one queue and one stack. 例如,Card原型包含一个队列和一个堆栈。

Thank you very much for any help you might give me. 非常感谢您为我提供的任何帮助。

It depends on what serialization you are using. 这取决于您使用的序列化。 Using ISerializable it works just fine. 使用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);
    }
}

Hope this helps. 希望这可以帮助。

You must implement default accessor for the Queue because the XMLSerializer need it to enum the items, and also the Add method. 您必须为队列实现默认访问器,因为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);   
    }
}

This is a Queue that could be serialized to XML. 这是一个可以序列化为XML的队列。

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

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