.net XmlSerializer on overridden properties

≡放荡痞女 提交于 2019-12-01 15:40:23
Vijay Sirigiri

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; }
}

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:

// Base class
protected int InternalID {get; set;}

// Derived class
[XmlElement]
public int SomethingID
{
  get {return InternalID;}
  set {InternalID = value;}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!