Code tree is like:
Class Data
{
List<Primitive> obj;
}
Class A: Primitive
{
ComplexType CTA;
}
Class B: A
{
ComplexType CTB;
Z o;
}
Class Z
{
ComplexType CTZ;
}
Class ComplexType { .... }
Now in List<Primitive> obj, there are many classes in which ComplexType object is ‘null’. I just want to initialize this to some value.
The problem is how to traverse the complete tree using reflection.
Edit:
Data data = GetData(); //All members of type ComplexType are null.
ComplexType complexType = GetComplexType();
I need to initialize all ‘ComplexType’ members in ‘data’ to ‘complexType’
If I understand you correctly perhaps something like this would do the trick:
And this method would be called like:
This code only works for fields of course. If you want properties as well you would have to have the loop iterate through all properties (which can be retreived by
instance.GetType().GetProperties(...)).Be aware though that reflection is not particularly efficient.