我有一个属性的名称,需要在类中找到它的值,获得该值的最快方法是什么?
最佳答案
我假设您在运行时拥有该属性的名称;不是在编码时...
假设您的类名为 TheClass,它有一个名为 TheProperty 的属性:
object GetThePropertyValue(object instance)
{
Type type = instance.GetType();
PropertyInfo propertyInfo = type.GetProperty("TheProperty");
return propertyInfo.GetValue(instance, null);
}
关于c# - 属性名称并需要它的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/798567/