根据属性字符串获取属性值
实体对象obj,如 User user=new User(), 属性名: propertyName 如UserName
根据属性字符串获取属性值:
public decimal GetPropertyValue(object obj, string propertyName)
{
if (obj == null)
return 0;
if (string.IsNullOrEmpty(propertyName))
return 0;
Type type = obj.GetType();
PropertyInfo property = type.GetProperty(propertyName);
if (property == null)
return 0;
return System.Convert.ToDecimal(property.GetValue(obj));
}

浙公网安备 33010602011771号