如何用字符串定位对象的属性?

时间:2012-07-05 10:14:42

标签: c# object reflection properties

如何动态地引用Employee类型对象的属性?我跟employee."hasBeenPaid"之类的东西?它涉及反思吗?

class Employee
{
    String name;
    Bool hasBeenPaid;
}

2 个答案:

答案 0 :(得分:5)

你可以尝试:

Type type = your_class.GetType();
PropertyInfo propinfo = type.GetProperty("hasBeenPaid");

如果您需要值

value = propinfo.GetValue(your_class, null);

答案 1 :(得分:2)

您可以使用dynamic C# feature;是的,它会在运行时使用反射来解析你的属性。

相关问题