I have a class:
public class Chid1 : Parent{
public string var1{get;set;}
public string var2{get;set;}
}
public class Chid2 : Parent{
public string var3{get;set;}
public string var4{get;set;}
}
I am collecting some data from an xml file and I need to setup these classes.
So this is what I can do for now:
string child = "Chid1";//from the xml
Parent instance = (Parent)Activator.CreateInstance(Type.GetType(child ) , ...);
Since Parent does not containg details about the childs, I cant do:
instance.var1 = "Some text from the XML";
Is there any solution here of setting up the childs via strings like:
Activator.SetParan(instance,"var1" , "Some text from the XML");
Or something else?
Have you considered using
dynamichere. It would make the code much simpler.Additionally is there a reason you’re not using a known serialization engine. If you are simply serializing objects to an from an XML stream there are a number of existing solutions that would make your life easier