用反射获取list<T>T对象的属性值 刚学反射,这样的该怎么写,给点代码参考下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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我该怎么改 Type tt = t.First<T>().GetType();//反射这个,不是反射IList 你要反射model中属性的属性值?一个model中有多个属性值啊property.GetValue(t, null) 这样想干嘛 反射出什么 IList<Model1> lm = new List<Model1>(); lm.Add(new Model1() { Age = 15, S = 131456, Email = "[email protected]" }); lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "[email protected]" }); 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; } } IList<Model1> lm = new List<Model1>(); lm.Add(new Model1() { Age = 15, S = 131456, Email = "[email protected]" }); lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "[email protected]" }); 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); } 小弟虚心请教,这个是不是获得所有属性的值,我想弄一个函数,获取一个指定的属性值,函数的形式就像这种public static string GetObjectPropertyValue(IList<T> list, string propertyname) 小弟虚心请教,这个是不是获得所有属性的值,我想弄一个函数,获取一个指定的属性值,函数的形式就像这种public static string GetObjectPropertyValue(IList<T> list, string propertyname)你一楼写的是什么? 你到底想要啥? 加个foreach,然后在foreach内部调用list<T>对象不就行了吗? 小弟虚心请教,这个是不是获得所有属性的值,我想弄一个函数,获取一个指定的属性值,函数的形式就像这种public static string GetObjectPropertyValue(IList<T> list, string propertyname)你一楼写的是什么? 你到底想要啥?我就是想通过函数,指定list<T>T的属性,然后返回一个string类型的属性值,比如我传入name这个参数,然后返回list里T.name的值 foreach(model m in listmodel){ var s= GetObjectPropertyValue(m,"name"); //然后你想干嘛就干嘛吧!} 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;} 怎么遍历treeview上的所有节点? .net MVC 泛型类 绑定到 html.dropdownlist 窗体排列 超可爱Remoting问题,答对满分相送 散分了散分了,新手来报道了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FarPoint Input Pro 各位帮帮忙,关于Windows Form的一个小问题。 在C#中如何继承控件 菜鸟问题 我一直钻研c#,没想到找工作居然还是找的dephi的,大家有什么建议? 用C#解析接收到的WITS协议数据,传输方式是UDP 生成目录树 datagridview中自写的textbox问题
{
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我该怎么改
一个model中有多个属性值啊property.GetValue(t, null) 这样想干嘛 反射出什么
lm.Add(new Model1() { Age = 15, S = 131456, Email = "[email protected]" });
lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "[email protected]" }); 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; }
}
lm.Add(new Model1() { Age = 15, S = 131456, Email = "[email protected]" });
lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "[email protected]" }); 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);
}
我就是想通过函数,指定list<T>T的属性,然后返回一个string类型的属性值,比如我传入name这个参数,然后返回list里T.name的值
{
var s= GetObjectPropertyValue(m,"name");
//然后你想干嘛就干嘛吧!
}
{
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;
}