Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net XmlSerializer on overridden properties

Tags:

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?

like image 367
TDaver Avatar asked Feb 04 '11 12:02

TDaver


1 Answers

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; } } 
like image 190
Vijay Sirigiri Avatar answered Sep 20 '22 05:09

Vijay Sirigiri