I've got the following structure of abstract classes:
public abstract class Template
{
// Some properties and methods defined
}
public abstract class Template<TTemplate> : Template where TTemplate : Template
{
// No new properties defined, but methods overriden
}
I then use these template classes as part of a model:
public abstract class Model
{
public Template Template {get;set;}
public Model(Template t) {Template = t;}
// More properties and methods
}
public abstract class Model<TModel, TTemplate> : Model where TModel : Model where TTemplate : Template
{
public new TTemplate template {get {return (TTemplate)base.Template;} set {base.Template = value;}}
public Model(TTemplate t) : base(t) {}
// Override some methods but no new properties
}
I then create concrete classes of my template and models and use them in my project. These concrete classes define additional properties beyond those specified in the abstract base classes. My problem comes when it's time to serialize the Model classes. I use reflection to find all inherited types of Model or Template, and pass them into the XmlSerializer so it can properly serialize my abstract classes. However, I get an exception
There was an error reflecting type **ConcreteModel**.
System.InvalidOperationException: There was an error reflecting property 'Template'. ---> System.InvalidOperationException: Member ModelOfConcreteModelConcreteTemplate.Template of type ConcreteTemplate hides base class member Model.Template of type Template. Use XmlElementAttribute or XmlAttributeAttribute to specify a new name.
I came across this post on google groups from 2003 which purports to give an answer, but I'm not sure how to implement that fix (or if it's even valid 13 years later). It does indicate that the error message is misleading as the solution proposed by the message does not work.
If I remove the 'set' accessor from the Model.Template and typed Model classes (and just set it via the constructor, for example), the class serializes just fine - albeit without the Template property. Is there a way to XML serialize classes which hide properties from a(n) (abstract) base class, without implementing IXmlSerializable on every single inherited class?