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

来源:互联网 发布:淘宝卖家骂人扣几分 编辑:程序博客网 时间:2024/05/20 04:12

blic 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 (== 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 />"); }

0 0
原创粉丝点击