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

liujiahaol123 2013-09-22 02:06:10
刚学反射,这样的该怎么写,给点代码参考下
...全文
855 14 打赏 收藏 转发到动态 举报
写回复
用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我该怎么改
该课程由付强老师主讲,系统的、全面的、具体的讲解了java入门的知识。可以使初级的学员入门。Java入门Java的历史、Java的版本、Java的优势、软件行业前景Java开发环境搭建、编写Java入门练习虚拟机的运行机制、Java的平台无关性、虚拟机垃圾回收机制基础语法关键字、标识符、Java数据类型分类、基础数据类型、取范围变量、常量、三种注释、生成doc文档、运算符、表达式if语句、switch语句、嵌套判断for语句、while语句、do-while语句、嵌套循环类和对象面向过程和面向对象的区别类的构成、访问修饰符、对象的内存分配this关键字、按传递与按引用传递、对象的初始化顺序高级类特性类的继承、父子类的初始化顺序、单继承性方法的覆盖、重载、super关键字、多态、instanceof关键字、强制类型转换、static关键字、final关键字数组和枚举一维数组的应用及内存分配多维数组的应用及内存分配数组的复制、数组的按引用传递排序算法:冒泡、直接选择、插入选择、希尔、快速、归并、堆排序Arrays工具类的应用枚举类型的应用常见类的使用Object类的底层及应用、Objects类String类的底层及应用、正则表达式StringBuffer和StringBuilder的底层及应用Math类的应用、包装类的应用日期类的应用:Date、DateFormat、Calendar、LocalDateTime、Instant、LocalDate、MonthDay、ZonedDateTime、YearMonth、新旧日期转换BigInteger和BingDecimal、DecimalFormatSystem类、Scanner类抽象类和接口抽象类的规则及应用接口的规则及应用默认方法、静态方法、函数式接口、Lambda表达式异常异常的定义异常的处理:抓(try-catch-finally)、抛(throws)异常的分类、自定义异常的应用、throw关键字集合框架集合框架结构:接口、实现类Collection接口的方法、Set接口的方法、List接口的方法、Map接口的方法Array、Linked、Hash、Tree底层实现原理泛型的作用、Collections工具类、历史集合类I/O流Path类的原理及应用、Files类的原理及应用文件字节流FileInputStream的原理及应用对象类、缓冲流、数据流的原理及应用字符流的原理及应用多线程进程与线程的概念、查看线程对象Java内存模型线程的创建与启动:扩展Thread类、实现Runnable接口、实现Callable接口、线程池线程状态的转换:新建、就绪、运行、阻塞、死亡线程的调度:sleep、yield、join、interrupt、wait后台线程、定时任务线程的并发与同步、同步锁、同步块、线程安全的类Lock接口、CAS、volatile修饰符内部类成员内部类、本地内部类的应用匿名内部类的原理及引用、Lambda表达式设计模式基础设计模式概念、框架概念7大设计原则:开闭原则、依赖倒置原则、单一职责原则、接口隔离原则、迪米特原则、里氏替换原则、合成复用原则单例模式、工厂模式、模板模式、代理模式、装饰模式、适配器模式、外观模式、策略模式、观察者模式、命令模式、备忘录模式、观察者模式反射反射包Class类的使用反射获取属性、方法、构造器通过反射创建类对象/通过反射调用方法反射的应用

110,571

社区成员

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

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

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