您的位置:首页 > 编程语言 > C#

C#利用反射获取对象属性值

2014-09-10 11:31 344 查看


C#利用反射获取对象属性值

public static string GetObjectPropertyValue<T>(T t, string propertyname)
{
Type type = typeof(T);      PropertyInfo property = type.GetProperty(propertyname);      if (property == null) return string.Empty;      object o = property.GetValue(t, null);      if (o == null) return string.Empty;      return o.ToString();
}

获取属性列表

Type t = typeof(Sit_showcase);
System.Reflection.PropertyInfo[] properties = t.GetProperties();

foreach (System.Reflection.PropertyInfo info in properties)
{
Response.Write("name=" + info.Name + ";" + "type=" + info.PropertyType.Name + ";value=" + GetObjectPropertyValue<Sit_showcase>(showcase, info.Name) + "<br />");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: