我有一個類叫做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)
我在其他職位,在當前的形勢下,這個類是因爲隊列不可序列讀取。這些信息可能已過時?我不知道,但我的問題是:
有沒有一種方法可以輕鬆地將這些隊列轉移到列表只是爲了序列化? 我需要爲這些成員所屬的所有班級做到這一點嗎?例如,卡原型包含一個隊列和一個堆棧。
非常感謝您爲我提供的任何幫助。
[Queue](http://msdn.microsoft.com/en-us/library/7977ey2c%28v=vs.110%29.aspx)有Serializable屬性,所以我不認爲這是原因你不能序列化。請添加你擁有的代碼(你寫道,你有他們的屬性,任何屬性?),例外,你使用什麼樣的序列化和你如何序列化。 –
Reniuz
2014-11-24 12:33:15
錯誤請 - 確切,不「喜歡」。 – TomTom 2014-11-24 12:35:57
我更新了我的帖子,並添加了我在調試過程中注意到的第二個異常。 – gheist 2014-11-24 15:24:18