.NET的XmlSerializer在被覆盖的特性被覆、特性、NET、XmlSerializer

2023-09-03 04:44:00 作者:啈冨^O^有伱

我有一个抽象属性的基类:

I have a base class with an abstract property:

public abstract int ID {get;set;}

现在,我有一个子类,这是XmlSerialized。所以,它具有:

now, I have a subclass, which is XmlSerialized. So, it has:

[XmlElement("something")]
public override int ID {
get { //... }
set { //... }
}

我无法移动的XmlElement属性基类,因为每个子类中都会有一个不同的XML的ElementName。

I cannot move the XmlElement attribute to baseclass, since every subclass will have a different xml elementname.

现在,当我反序列化这个类,我得到了以下错误:

Now, when I deserialize this class I get the following error:

会员Subclass.ID隐藏继承   成员BaseClass.ID',但   不同的自定义属性。

Member 'Subclass.ID' hides inherited member 'BaseClass.ID', but has different custom attributes.

我该怎么办?

推荐答案

序列化和派生类型的反序列化工作时,覆盖的属性有 [的XmlElement] [XmlAttribute] 属性,通过添加一个 [XmlIgnore] 属性。

Serialization and deserialization of derived types works when the overridden properties have [XmlElement] and [XmlAttribute] attributes, by adding an [XmlIgnore] attribute.

基类可以由抽象,使得它永远被实例化,因此,串行化或并行化。

The base class can be made abstract so that it can never be instantiated and therefore serialized or deserialized.

[Serializable]
public abstract class Base
{
    [XmlIgnore]
    public abstract Int32 ID { get; set; }
}