序列化具有接口的对象
Serialize an object that has an interface
我的 XML 序列化问题很麻烦。我一直在研究我的项目,以(反)序列化一个具有接口作为属性的对象。我知道你不能序列化一个接口,这就是我的错误告诉我的。
这是我要保存到文件的对象的示例:
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 (Mouse)] 用于我的每个使用此接口的 类。
这是我的问题:为什么当我尝试序列化时,它无法检测到我的对象类型(任务中的 typeEntree),因为我添加了 [XmlInclude (typeof (鼠标)]属性?
还有,我该如何解决这个问题?
此外,这里是 serializing/deserializing 的方法,我发现它似乎在没有界面的情况下工作得很好:
多亏了我第一个问题评论中的@dbc 链接,我才能够弄清楚每一个问题。这是我所做的:
我的接口 TypeEntree 变成了抽象的 class。
[Serializable]
[XmlInclude(typeof(Mouse))]
[XmlInclude(typeof(Keyboard))]
[XmlInclude(typeof(Sequence))]
public abstract class TypeEntree
{
}
此外,鼠标 class 有一个不可序列化的 IntPtr。我不得不将它转换为 Int64(长整数)。来源均来自@dbc 评论和此处:Serialize an IntPtr using XmlSerializer
最后,Tuple 不能序列化,因为它没有无参数构造函数。解决这个问题的方法是简单地将元组的类型更改为我在这个例子之后创建的 class (TupleModifier):
public class TupleModifier<T1, T2, T3, T4>
{
public T1 Item1 { get; set; }
public T2 Item2 { get; set; }
public T3 Item3 { get; set; }
public T4 Item4 { get; set; }
public TupleModifier() { }
public static implicit operator TupleModifier<T1, T2, T3, T4>(Tuple<T1, T2, T3, T4> t)
{
return new TupleModifier<T1, T2, T3, T4>()
{
Item1 = t.Item1,
Item2 = t.Item2,
Item3 = t.Item3,
Item4 = t.Item4
};
}
public static implicit operator Tuple<T1, T2, T3, T4>(TupleModifier<T1, T2, T3, T4> t)
{
return Tuple.Create(t.Item1, t.Item2, t.Item3, t.Item4);
}
}
并在序列 class 中像使用它一样使用它:
public List<TupleModifier<string, Point, long, long>> actions;
我的 XML 序列化问题很麻烦。我一直在研究我的项目,以(反)序列化一个具有接口作为属性的对象。我知道你不能序列化一个接口,这就是我的错误告诉我的。
这是我要保存到文件的对象的示例:
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 (Mouse)] 用于我的每个使用此接口的 类。
这是我的问题:为什么当我尝试序列化时,它无法检测到我的对象类型(任务中的 typeEntree),因为我添加了 [XmlInclude (typeof (鼠标)]属性?
还有,我该如何解决这个问题?
此外,这里是 serializing/deserializing 的方法,我发现它似乎在没有界面的情况下工作得很好:
多亏了我第一个问题评论中的@dbc 链接,我才能够弄清楚每一个问题。这是我所做的:
我的接口 TypeEntree 变成了抽象的 class。
[Serializable]
[XmlInclude(typeof(Mouse))]
[XmlInclude(typeof(Keyboard))]
[XmlInclude(typeof(Sequence))]
public abstract class TypeEntree
{
}
此外,鼠标 class 有一个不可序列化的 IntPtr。我不得不将它转换为 Int64(长整数)。来源均来自@dbc 评论和此处:Serialize an IntPtr using XmlSerializer
最后,Tuple 不能序列化,因为它没有无参数构造函数。解决这个问题的方法是简单地将元组的类型更改为我在这个例子之后创建的 class (TupleModifier):
public class TupleModifier<T1, T2, T3, T4>
{
public T1 Item1 { get; set; }
public T2 Item2 { get; set; }
public T3 Item3 { get; set; }
public T4 Item4 { get; set; }
public TupleModifier() { }
public static implicit operator TupleModifier<T1, T2, T3, T4>(Tuple<T1, T2, T3, T4> t)
{
return new TupleModifier<T1, T2, T3, T4>()
{
Item1 = t.Item1,
Item2 = t.Item2,
Item3 = t.Item3,
Item4 = t.Item4
};
}
public static implicit operator Tuple<T1, T2, T3, T4>(TupleModifier<T1, T2, T3, T4> t)
{
return Tuple.Create(t.Item1, t.Item2, t.Item3, t.Item4);
}
}
并在序列 class 中像使用它一样使用它:
public List<TupleModifier<string, Point, long, long>> actions;