2014-11-24 66 views
0

我有一个类叫做GamePrototype,我想序列化XML的使用,包括下列成员:如何序列化一个包含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; 
} 

我一直在试图用这种方法序列化类数据。 “这”指的是上面提到的类的实例:

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) 

我在其他职位,在当前的形势下,这个类是因为队列不可序列读取。这些信息可能已过时?我不知道,但我的问题是:

有没有一种方法可以轻松地将这些队列转移到列表只是为了序列化? 我需要为这些成员所属的所有班级做到这一点吗?例如,卡原型包含一个队列和一个堆栈。

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

+0

[Queue ](http://msdn.microsoft.com/en-us/library/7977ey2c%28v=vs.110%29.aspx)有Seri​​alizable属性,所以我不认为这是原因你不能序列化。请添加你拥有的代码(你写道,你有他们的属性,任何属性?),例外,你使用什么样的序列化和你如何序列化。 – Reniuz 2014-11-24 12:33:15

+0

错误请 - 确切,不“喜欢”。 – TomTom 2014-11-24 12:35:57

+0

我更新了我的帖子,并添加了我在调试过程中注意到的第二个异常。 – gheist 2014-11-24 15:24:18

回答

0

这取决于您正在使用的序列化。 使用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); 
    } 
} 

希望这会有所帮助。

0

您必须实现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的队列。