lundi 9 février 2015

How can I find base class properties that have their properties hidden by derived type

I am developing a code generator that builds classes and then later has these classes serialized to XML through the XmlSerializer.


The problem is some of my base classes have defined properties but some of these properties will be hidden by derived types using the 'new' keyword in the derived types. This causes issues with serialization of the types because the Serializer doesn't allow for the types to be serialized unless the base type has an [XmlIgnore] attribute specified on the property that will be hidden by the derived type. The problem is that I do not know this until after I generate the code.


I was thinking that their must be a good way to use reflection to find all of the derived types of the base class I am generating and if any of the derived types hide the member or have a different custom attribute, then I would generate a [XmlIgnore] attribute on this property so that the serializer will not choke.


There must be a relatively simple way to do this as the compiler knows at compile time which base class properties are hidden and provides a warning if you are not specifying the 'new' keyword to tell the compiler that that is what is intended.


Any ideas of an elegant way that I could go about this?






Aucun commentaire:

Enregistrer un commentaire