lundi 30 mars 2015

C# Accessing a nested variable by name

I multiple classes with the following nested structure -



class A
{
public B b;
public C c;
}

class B
{
int count;
}

class C
{
public d D
}

class D
{
string name;
}


I need to access the deepest level variables of multiple such object structures at runtime. I will not know the structure of the class beforehand, and I will need to repeatedly do this over and over.


My approach was to use reflection string to variable name and store the object structure, however knowing the object structure is there any way I can access it faster or will I have have to use multiple level reflection? For example to access name I would do the following via c# code - a.c.d.name, but by using reflection -GetType(A).GetMember(c).GetType().GetMember(d).GetType().GetMember(name).SetValue(blah);


Any suggestions?


Aucun commentaire:

Enregistrer un commentaire