我想獲取從接口繼承的對象列表並將它們序列化爲xml(JSON也可接受)。這些對象中的每一個都從相同的抽象類繼承,並且遵循相同的接口。如何序列化/反序列化從普通類繼承的對象列表並遵循同一個接口?
請參見下面的類:
public abstract class Item
{
public string Name { get; set; }
public int SellIn { get; set; }
public int Quality { get; set; }
public Item(string name, int sellIn, int quality)
{
Name = name;
SellIn = sellIn;
Quality = quality;
}
public virtual void UpdateQuality()
{
//Default Behaviour
}
}
//子類
public class Appreciative : Item, IItem
{
public Appreciative(string name, int sellIn, int quality) : base(name, sellIn, quality)
{}
public override void UpdateQuality()
{
//Sub class behaviour
}
}
XmlSerializer類無法確定每個類的序列化/反序列化的時候,我明白了類型爲什麼,但我期待解決這個問題。
唯一的成功到目前爲止我已經在使用它已成功序列化的數據如下ExtendedXmlSerializer:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfIItem>
<Depreciative type="CodingChallenge3.Internals.Items.Depreciative">
<Name>+5 Dexterity Vest</Name>
<SellIn>9</SellIn>
<Quality>18</Quality>
</Depreciative>
<Appreciative type="CodingChallenge3.Internals.Items.Appreciative">
<Name>Aged Brie</Name>
<SellIn>1</SellIn>
<Quality>1</Quality>
</Appreciative>
<Depreciative type="CodingChallenge3.Internals.Items.Depreciative">
<Name>Elixir of the Mongoose</Name>
<SellIn>4</SellIn>
<Quality>5</Quality>
</Depreciative>
<Fixed type="CodingChallenge3.Internals.Items.Fixed">
<Name>Sulfuras, Hand of Ragnaros</Name>
<SellIn>0</SellIn>
<Quality>80</Quality>
</Fixed>
<TicketEvent type="CodingChallenge3.Internals.Items.TicketEvent">
<Name>Backstage passes to a TAFKAL80ETC concert</Name>
<SellIn>14</SellIn>
<Quality>20</Quality>
</TicketEvent>
<Depreciative type="CodingChallenge3.Internals.Items.Depreciative">
<Name>Conjured Mana Cake</Name>
<SellIn>2</SellIn>
<Quality>4</Quality>
</Depreciative>
</ArrayOfIItem>
的類型在type屬性指定,但ExtendededXmlSerializer失敗反序列化XML,說明空引用異常。
以下是讀取和寫入數據的存儲庫代碼。
var serializedData = new ExtendedXmlSerializer().Serialize(inventory);
System.IO.File.WriteAllText(PATH, serializedData);
var inventory = new List<IItem>();
if (System.IO.File.Exists(PATH))
{
var serializedData = System.IO.File.ReadAllText(PATH);
inventory = new ExtendedXmlSerializer().Deserialize<List<IItem>>(serializedData);
}
我已閱讀堆棧溢出的類似問題,但沒有符合我的(到目前爲止)。
編輯:堆棧跟蹤
at ExtendedXmlSerialization.ExtendedXmlSerializer.ReadXml(XElement currentNode, TypeDefinition type, Object instance)
at ExtendedXmlSerialization.ExtendedXmlSerializer.ReadXmlArray(XElement currentNode, TypeDefinition type, Object instance)
at ExtendedXmlSerialization.ExtendedXmlSerializer.ReadXml(XElement currentNode, TypeDefinition type, Object instance)
at ExtendedXmlSerialization.ExtendedXmlSerializer.Deserialize(String xml, Type type)
at ExtendedXmlSerialization.ExtendedXmlSerializer.Deserialize[T](String xml)
嗯,我希望你不會不高興;)......但有趣的是,這只是c&p或是你的抽象物品類沒有實施IItem?你能添加異常的堆棧跟蹤嗎? – Fildor
我改變了抽象類來實現IItem建議,但這並沒有改變結果。現在添加堆棧跟蹤 –
如果您在創建序列化程序時包含相關類型(過載[像這樣一個](https://msdn.microsoft.com/en-us/library/e5aakyae( v = vs.110).aspx)允許你指定序列化器應該處理的所有類型)。 –