110,571
社区成员
发帖
与我相关
我的任务
分享
public static string GetObjectPropertyValue(IList<T> list, string propertyname)
{
string str="";
foreach(T t in list)
{
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;
str= o.ToString();
}
return str;
}
[quote=引用 8 楼 liujiahaol123 的回复:] [quote=引用 7 楼 zhuankeshumo 的回复:]IList<Model1> lm = new List<Model1>(); lm.Add(new Model1() { Age = 15, S = 131456, Email = "121212@qq.com" }); lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "121212@qq.com" }); IList<Dictionary<string, object>> ld = new List<Dictionary<string, object>>(); PropertyInfo[] props = typeof(Model1).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (Model1 _model in lm) { Dictionary<string, object> values = new Dictionary<string, object>(); foreach (PropertyInfo property in props) { values.Add(property.Name, property.GetValue(_model, property.GetIndexParameters())); } ld.Add(values); }
[quote=引用 7 楼 zhuankeshumo 的回复:]IList<Model1> lm = new List<Model1>(); lm.Add(new Model1() { Age = 15, S = 131456, Email = "121212@qq.com" }); lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "121212@qq.com" }); IList<Dictionary<string, object>> ld = new List<Dictionary<string, object>>(); PropertyInfo[] props = typeof(Model1).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (Model1 _model in lm) { Dictionary<string, object> values = new Dictionary<string, object>(); foreach (PropertyInfo property in props) { values.Add(property.Name, property.GetValue(_model, property.GetIndexParameters())); } ld.Add(values); }
IList<Model1> lm = new List<Model1>(); lm.Add(new Model1() { Age = 15, S = 131456, Email = "121212@qq.com" }); lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "121212@qq.com" }); IList<Dictionary<string, object>> ld = new List<Dictionary<string, object>>(); PropertyInfo[] props = typeof(Model1).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (Model1 _model in lm) { Dictionary<string, object> values = new Dictionary<string, object>(); foreach (PropertyInfo property in props) { values.Add(property.Name, property.GetValue(_model, property.GetIndexParameters())); } ld.Add(values); }
IList<Model1> lm = new List<Model1>();
lm.Add(new Model1() { Age = 15, S = 131456, Email = "121212@qq.com" });
lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "121212@qq.com" });
IList<Dictionary<string, object>> ld = new List<Dictionary<string, object>>();
PropertyInfo[] props = typeof(Model1).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
foreach (Model1 _model in lm)
{
Dictionary<string, object> values = new Dictionary<string, object>();
foreach (PropertyInfo property in props)
{
values.Add(property.Name, property.GetValue(_model, property.GetIndexParameters()));
}
ld.Add(values);
}
IList<Model1> lm = new List<Model1>();
lm.Add(new Model1() { Age = 15, S = 131456, Email = "121212@qq.com" });
lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "121212@qq.com" });
IList<Dictionary<string, object>> ld = new List<Dictionary<string, object>>();
foreach (Model1 _model in lm)
{
PropertyInfo[] props =_model.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
Dictionary<string, object> values = new Dictionary<string, object>();
foreach (PropertyInfo property in props)
{
values.Add(property.Name, property.GetValue(model, property.GetIndexParameters()));
}
ld.Add(values);
}
public class Model1
{
public string Name { get; set; }
public int Age { get; set; }
public int S { get; set; }
public string Email { get; set; }
}
刚学反射,这样的该怎么写,给点代码参考下
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();
}
这个是类的,我把他改成
public static string GetObjectPropertyValue(IList<T> list, string propertyname)
{
Type type = list.GetType();
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();
}
object o = property.GetValue(t, null);里的t我该怎么改