如何在c#中使用反射从类的成员中获取对象值?
我:
public class Class1 {
public int field1;
public String field2;
}
public class Class2 {
public ind code;
public Class1 classRef;
}
我想获得成员列表的"classRef"值
我觉得这很接近
var c2 = new Class2
{
code = 3;
Class1 = new Class1
{
field1 = 7;
field2 = "class 1"
}
};
var fields = c2.GetType().GetFields();
var field = fields.Where(fi=>fi.Name == "classRef").FirstOrDefault();
Assert.True(field != null);
var value = field.GetValue(c2) as Class1;
Assert.True(value != null);
Assert.True(value.field1 == 7);
Assert.True(value.field2 == "class 1");