獲取一個類指定的屬性值
阿新 • • 發佈:2018-11-24
/// <summary> /// 獲取一個類指定的屬性值 /// </summary> /// <param name="info">object物件</param> /// <param name="field">屬性名稱</param> /// <returns></returns> public static object GetPropertyValue(object info, string field) {try { if (info == null) return null; Type t = info.GetType(); IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi; return property.First().GetValue(info, null); } catch (Exception ex) { string msg = ex.Message.ToString(); return null; } }