I have a xml file with data from various sources. For example:
<root>
<Account>
<Id>1</Id>
<Name>Somename</Name>
<City>Somecity</City>
<ContactPersons>
<ContactPerson>
<Id>1</Id>
<Name>Somename</Name>
</ContactPerson>
<ContactPerson>
<Id>2</Id>
<Name>Somename2</Name>
</ContactPerson>
</ContactPersons>
</Account>
</root>
I created a generic businesscollection (named BusinessCollection) and a business class (named BusinessObject).
Then I created a specific business class named Account en another one called ContactPerson. They both derive from the BusinessObject class.
I created a instance of a businesscollection with accounts using:
BusinessCollection <Account> collection = new BusinessCollection <Account>();
I also made a parser that 'translates' the xml into the correct business classes. So in case of the data above I
provide a instance of BusinessCollection <Account>. Then I create Account objects and add them to the collection. This is all done
using reflection and some XPath classes.
The parser has the folling method signature:
public static void ParseToBusinessObjects <T>(BusinessCollection <T> collection, XPathDocument document) where T : BusinessObject
So far so good. But now I also want subcollections to parse. In the xml data ContactPersons will be my subcollection (property is named the same).
If my parser detects a subcollection it will get the current instance (using reflection). I then want to call my entry method again for this collection (recursive)
However when I do this:
BusinessCollection <BusinessObject> subCollection = (BusinessCollection <BusinessObject>) property.GetValue(instance, null);
I get a InvalidCastException saying I can't convert between a BusinessCollection<ContactPerson> and a BusinessCollection<BusinessObject>.
Its the same when I change it to :
BusinessCollection <T> subCollection = (BusinessCollection <T>) property.GetValue(instance, null);
I hope someone knows how to solve this because its driving me nuts.