Reflection for objects declared in a type-Collection of common programming errors
I have two classes, a parent and a child class.
public class Parent
{
public Parent()
{
}
public object getVariableValue(object instance, string variableName)
{
return instance.GetType().GetField(variableName).GetValue(instance);
}
}
public
class Employees
{
public string name;public int salary;public Employees()
{
}
}
public class Child : Parent
{
public Employees emp1;public int x;public Child()
{
emp1 =
new Employees();
emp1.name = “jhon”;
emp1.salary = 1200;
x = 2323;
}
}
when I try to get the value of a field using reflection, it works successfully
Child cc = new Child();object o = cc.getVariableValue(cc,”x”);
now I need to get the value of the object “emp” members (name and salary). is there a way to do that?
m gado