如何根据名称来获取属性值 [英] How to get a property value based on the name
本文介绍了如何根据名称来获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法得到一个对象的属性的基础上它的名字的价值?
例如,如果我有:
公共类车:车辆
{
公共字符串制作{搞定;组; }
}
和
VAR车=新车{使=福特};
我想写,我可以在属性名传递的方法,它会返回属性值。即:
公共字符串GetPropertyValue(字符串propertyName的)
{
返回的属性的值;
}
解决方案
返回car.GetType()的getProperty(propertyName的).GetValue(汽车,NULL);
is there a way to get the value of a property of a object based on its name?
For example if I have:
public class Car : Vehicle
{
public string Make { get; set; }
}
and
var car = new Car { Make="Ford" };
I want to write a method where I can pass in the property name and it would return the property value. ie:
public string GetPropertyValue(string propertyName)
{
return the value of the property;
}
解决方案
return car.GetType().GetProperty(propertyName).GetValue(car, null);
这篇关于如何根据名称来获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文