XML Serialization with Inheritance and “new”

We were building an XML structure … we will call this a Rule and a DerivedRule.

We built simple classes to serialize or deserialize the XML.

At the basic level all that works fine … my DerivedRule will have everything my Rule has and the “Value” string.

The issue arose when we had multiple Rules or DerivedRules … We made a simple LIST classes following the same style:

In theory we would think this work work … RuleList was the base class and DerivedRuleList would just extend RuleList.  The problem was that we were extending the Rules property … but HIDING the base classes property using “new”

When we tried to deserialize this we would get the following error:


We were baffled for a while, because we were appropriately utilizing “XmlElement” as requested.  And the we read the error in far more detail … the Rules property was hiding the base class member’s Rules property.  We couldn’t just override it, and because the DerivedRuleList was using a List<DerivedRule> and the RulesList was using a List<Rule>.

After searching for a while, we didn’t find a solution and our final output was that we disconnected the inheritance on the DerivedRuleList and everything came up without issue.


Other attempts were made and some compiled just fine, but wouldn’t actually serialize or deserialize … and that was to make Rule and DerivedRule implement a common interface and then make a SINGLE RulesList with a property of List<IRule> Rules … and while that correctly compiles without any issue, serialization won’t work with an interface without much additional coding.



, , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: