我遇到了與我的XML序列化問題相當的問題。我一直在研究我的項目(de)序列化一個具有接口作爲屬性的對象。我知道你不能序列化一個接口,這就是我的錯誤告訴我的。序列化具有接口的對象
這裏是我想保存到一個文件對象的例子:
public class Task
{
public int id;
public string name;
public TypeEntree typeEntree;
public int idRequired;
public string code;
public int waitTime;
public string nameApp;
// ... Constructors (empty and non-empty) and methods ...
}
TypeEntree是一個空的接口,它只是涉及到不同的對象,方便地使用他們在我的應用程序。例如,這裏有兩個對象使用此接口:
[Serializable]
public class Mouse : TypeEntree
{
public Point point;
public IntPtr gaucheOuDroite;
public string image;
// ... Constructors (empty and non-empty) and methods ...
}
[Serializable]
public class Sequence : TypeEntree
{
public List<Tuple<string, Point, long, IntPtr>> actions;
// ... Constructors (empty and non-empty) and methods ...
}
接口TypeEntree也有[Serializable]屬性,也是[XmlInclude(typeof運算(鼠標)]我的每個類使用該。接口
這裏是我的問題:爲什麼當我試圖序列,它無法檢測到我的對象的類型(在任務typeEntree),因爲我加入了[XmlInclude(typeof運算(鼠標)屬性
?另外,我該如何解決這個問題?
Additionnally,這裏有串行/解串的方法,我發現,似乎工作得非常好無接口:https://stackoverflow.com/a/22417240/6303528
你使用哪個序列化程序? – rene
在最後一句中,我指定了它。 http://stackoverflow.com/a/22417240/6303528 - XML XmlSerializer –
序列化程序添加不用於序列化的屬性,但會反序列化。網絡庫在反序列化時需要屬性來區分繼承的類。 – jdweng