C#利用反射獲取對象屬性值

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章