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.