简体   繁体   English

C#不要序列化继承的类

[英]C# don't serialize inherited class

I have a class that keeps certain data (for a game editor), for its use in my program it must inherit the class System.Windows.Controls.Image . 我有一个保留某些数据的类(用于游戏编辑器),要在我的程序中使用它,它必须继承System.Windows.Controls.Image类。

The class looks like this: 该类如下所示:

public class GameObjectInstance : Image
{
    public string InstanceName { get; set; }
    public string ObjectX { get; set; }
    public string ObjectY { get; set; }
    ...
}

Image must be inherited because I must draw the GameObjectInstance on a Canvas to display the image but I must also retain the specific GameObjectInstance data. 必须继承Image因为我必须在Canvas上绘制GameObjectInstance才能显示图像,但我还必须保留特定的GameObjectInstance数据。

My program must save a number of these GameObjectInstance classes to files somewhere. 我的程序必须将许多这些GameObjectInstance类保存到文件中的某个位置。 I am trying to do this via serialization, and I have discovered that the XmlSerializer doesn't support this. 我正在尝试通过序列化完成此操作,但我发现XmlSerializer不支持此功能。 I have also tried serializing using the BinaryFormatter but that does not work either. 我也尝试过使用BinaryFormatter序列化,但这也不起作用。

How can I serialize so that I ignore the inherited properties (I don't need their data saved)? 如何序列化以便忽略继承的属性(不需要保存其数据)?

Don't inherit from image but rather define a property of type Image in your class. 不要从image继承,而是在类中定义Image类型的属性。 Use [XmlIgnore] to mark the properties that you do not want to serialize. 使用[XmlIgnore]标记您不想序列化的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM