用反射获取listT对象的属性值

liujiahaol123 2013-09-22 02:06:10
刚学反射,这样的该怎么写,给点代码参考下
...全文
960 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ajdkjalj 2013-09-22
  • 打赏
  • 举报
回复
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;
}
liujiahaol123 2013-09-22
  • 打赏
  • 举报
回复
引用 12 楼 zhuankeshumo 的回复:
foreach(model m in listmodel) { var s= GetObjectPropertyValue(m,"name"); //然后你想干嘛就干嘛吧! }
引用 11 楼 liujiahaol123 的回复:
真心感谢
newtee 2013-09-22
  • 打赏
  • 举报
回复
foreach(model m in listmodel) { var s= GetObjectPropertyValue(m,"name"); //然后你想干嘛就干嘛吧! }
引用 11 楼 liujiahaol123 的回复:
liujiahaol123 2013-09-22
  • 打赏
  • 举报
回复
引用 9 楼 zhuankeshumo 的回复:
[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);
            }
小弟虚心请教,这个是不是获得所有属性的值,我想弄一个函数,获取一个指定的属性值,函数的形式就像这种public static string GetObjectPropertyValue(IList<T> list, string propertyname)[/quote]你一楼写的是什么? 你到底想要啥?[/quote] 我就是想通过函数,指定list<T>T的属性,然后返回一个string类型的属性值,比如我传入name这个参数,然后返回list里T.name的值
newtee 2013-09-22
  • 打赏
  • 举报
回复
加个foreach,然后在foreach内部调用list<T>对象不就行了吗?
newtee 2013-09-22
  • 打赏
  • 举报
回复
引用 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);
            }
小弟虚心请教,这个是不是获得所有属性的值,我想弄一个函数,获取一个指定的属性值,函数的形式就像这种public static string GetObjectPropertyValue(IList<T> list, string propertyname)[/quote]你一楼写的是什么? 你到底想要啥?
liujiahaol123 2013-09-22
  • 打赏
  • 举报
回复
引用 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);
            }
小弟虚心请教,这个是不是获得所有属性的值,我想弄一个函数,获取一个指定的属性值,函数的形式就像这种public static string GetObjectPropertyValue(IList<T> list, string propertyname)
newtee 2013-09-22
  • 打赏
  • 举报
回复
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);
            }
newtee 2013-09-22
  • 打赏
  • 举报
回复
  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; }
    }
newtee 2013-09-22
  • 打赏
  • 举报
回复
引用 楼主 liujiahaol123 的回复:
刚学反射,这样的该怎么写,给点代码参考下
你要反射model中属性的属性值? 一个model中有多个属性值啊property.GetValue(t, null) 这样想干嘛 反射出什么
jiaoshiyao 2013-09-22
  • 打赏
  • 举报
回复
发错Sorry
全栈极简 2013-09-22
  • 打赏
  • 举报
回复
Type tt = t.First<T>().GetType();//反射这个,不是反射IList
liujiahaol123 2013-09-22
  • 打赏
  • 举报
回复
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我该怎么改

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧