本文介绍了.NET的XmlSerializer在被覆盖的特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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',但  不同的自定义属性。

我该怎么办?

推荐答案

序列化和派生类型的反序列化工作时,覆盖的属性有 [的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在被覆盖的特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:24