static T GetObjectPropertyValue<T>(IList<T> list, string propertyname, object propertyvalue) { Type type = typeof(T); foreach (T t in list) { type = typeof(T); PropertyInfo property = type.GetProperty(propertyname); if (property == null) return t; object o = property.GetValue(t, null); if (o == null) return t; if (propertyvalue.ToString() == o.ToString()) return t; } return default(T); }
除去反射,应该是没办法知道T是否有id属性的。
{
Type type = typeof(T);
foreach (T t in list)
{
type = typeof(T); PropertyInfo property = type.GetProperty(propertyname); if (property == null) return t; object o = property.GetValue(t, null); if (o == null) return t; if (propertyvalue.ToString() == o.ToString())
return t;
}
return default(T);
}