.net XmlSerializer 覆盖属性
我有一个带有抽象属性的基类:
public abstract int ID {get;set;}
现在,我有一个子类,它是 XmlSerialized。因此,它具有:
[XmlElement("something")]
public override int ID {
get { //... }
set { //... }
}
我无法将 XmlElement 属性移至基类,因为每个子类都有不同的 xml 元素名称。
现在,当我反序列化此类时,出现以下错误:
成员“Subclass.ID”隐藏继承的 成员“BaseClass.ID”,但有 不同的自定义属性。
我能做些什么?
I have a base class with an abstract property:
public abstract int ID {get;set;}
now, I have a subclass, which is XmlSerialized. So, it has:
[XmlElement("something")]
public override int ID {
get { //... }
set { //... }
}
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:
Member 'Subclass.ID' hides inherited
member 'BaseClass.ID', but has
different custom attributes.
What can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当重写的属性具有
[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.
使基类属性受保护且非抽象,然后为每个派生类提供一个根据基类属性实现的适当命名的属性:
Make the base class property protected and non-abstract, then give each derived class an appropriately named property implemented in terms of the base class property: