.net XmlSerializer覆盖的属性 [英] .net XmlSerializer on overridden properties
问题描述
我有一个带有抽象属性的基类:
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元素名称.
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; }
}
这篇关于.net XmlSerializer覆盖的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!