C# 反射泛型对象得到属性和值

cym30 2013-05-21 10:43:05
有一个泛型对象List<User> user=new List<User>();

我想通过反射得到user对象的值和属性名称。请高人指点一二,给出具体代码。
...全文
1381 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄色不倒翁 2014-05-09
  • 打赏
  • 举报
回复
就是一个获取getpropertys(),然后循环输出
oreoconansisu 2013-05-22
  • 打赏
  • 举报
回复
引用 8 楼 caozhy 的回复:
[quote=引用 7 楼 oreoconansisu 的回复:] [quote=引用 6 楼 caozhy 的回复:] [quote=引用 5 楼 oreoconansisu 的回复:]
foreach (User u1 in user)
            {
                Type t = u1.GetType();

                foreach (var prop in t.GetProperties())
                {
                    string name = prop.Name;
                }
            }
傻不傻 如果你知道是User foreach (User u1 in user) 直接typeof(User)不就好了。[/quote] 学艺不精,见笑了 重新改了

Type t = typeof(User);

PropertyInfo[] propInfos = t.GetProperties();

List<User> user = new List<User>();

foreach (User u1 in user)
{
    foreach (var pi in propInfos)
    {
        string name = pi.Name;
        object value = pi.GetValue(u1, null);
    }
}
[/quote] 呵呵,那还需要 List<User> user = new List<User>(); foreach (User u1 in user) 么?[/quote] 这个多写了一步,通过反射取属性值
threenewbee 2013-05-22
  • 打赏
  • 举报
回复
引用 7 楼 oreoconansisu 的回复:
[quote=引用 6 楼 caozhy 的回复:] [quote=引用 5 楼 oreoconansisu 的回复:]
foreach (User u1 in user)
            {
                Type t = u1.GetType();

                foreach (var prop in t.GetProperties())
                {
                    string name = prop.Name;
                }
            }
傻不傻 如果你知道是User foreach (User u1 in user) 直接typeof(User)不就好了。[/quote] 学艺不精,见笑了 重新改了

Type t = typeof(User);

PropertyInfo[] propInfos = t.GetProperties();

List<User> user = new List<User>();

foreach (User u1 in user)
{
    foreach (var pi in propInfos)
    {
        string name = pi.Name;
        object value = pi.GetValue(u1, null);
    }
}
[/quote] 呵呵,那还需要 List<User> user = new List<User>(); foreach (User u1 in user) 么?
threenewbee 2013-05-22
  • 打赏
  • 举报
回复
引用 14 楼 cym30 的回复:
[quote=引用 13 楼 caozhy 的回复:] 获得值只需要在我的代码上加上 item.GetValue(user) 即可。
你这样只能得到List里的第一个对象,List.Add(A),List.Add(B) 泛型中有2个数据对象的时候只能得到第一个。[/quote] 我是举例啊 你可以用 foreach (var obj in (list As IEnumerable)) item.GetValue(obj, null);
cym30 2013-05-22
  • 打赏
  • 举报
回复
引用 13 楼 caozhy 的回复:
获得值只需要在我的代码上加上 item.GetValue(user) 即可。
你这样只能得到List里的第一个对象,List.Add(A),List.Add(B) 泛型中有2个数据对象的时候只能得到第一个。
threenewbee 2013-05-22
  • 打赏
  • 举报
回复
获得值只需要在我的代码上加上 item.GetValue(user) 即可。
threenewbee 2013-05-22
  • 打赏
  • 举报
回复
引用 10 楼 cym30 的回复:
[quote=引用 4 楼 caozhy 的回复:]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class User
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string EMail { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            object list = new List<User>();
            var properties = list.GetType().GetGenericArguments()[0].GetProperties();
            foreach (var item in properties)
                Console.WriteLine(item);
        }
    }
}
System.String Name Int32 Age System.String EMail Press any key to continue . . .
List<User> list = new List<User>(); User user= new User(); user.Id = "1"; user.Name = "张三"; list.Add(user); User tUser = new User (); tUser.Id = "2"; tUser.Name = "李四"; list.Add(tUser); 我怎么反射得到list对象下的Id,Name的名称和值。 [/quote] 都给你代码了。
cym30 2013-05-22
  • 打赏
  • 举报
回复
引用 4 楼 caozhy 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class User
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string EMail { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            object list = new List<User>();
            var properties = list.GetType().GetGenericArguments()[0].GetProperties();
            foreach (var item in properties)
                Console.WriteLine(item);
        }
    }
}
System.String Name Int32 Age System.String EMail Press any key to continue . . .
List<User> list = new List<User>(); User user= new User(); user.Id = "1"; user.Name = "张三"; list.Add(user); User tUser = new User (); tUser.Id = "2"; tUser.Name = "李四"; list.Add(tUser); 我怎么反射得到list对象下的Id,Name的名称和值。
cym30 2013-05-22
  • 打赏
  • 举报
回复
引用 4 楼 caozhy 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class User
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string EMail { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            object list = new List<User>();
            var properties = list.GetType().GetGenericArguments()[0].GetProperties();
            foreach (var item in properties)
                Console.WriteLine(item);
        }
    }
}
System.String Name Int32 Age System.String EMail Press any key to continue . . .
List<User> list = new List<User>(); User user= new User(); user.Id = "1"; user.Name = "张三"; list.Add(user); User tUser = new User (); tUser.Id = "2"; tUser.Name = "李四"; list.Add(tUser); 我怎么反射得到list对象下的Id,Name的名称和值。
oreoconansisu 2013-05-21
  • 打赏
  • 举报
回复
引用 6 楼 caozhy 的回复:
[quote=引用 5 楼 oreoconansisu 的回复:]
foreach (User u1 in user)
            {
                Type t = u1.GetType();

                foreach (var prop in t.GetProperties())
                {
                    string name = prop.Name;
                }
            }
傻不傻 如果你知道是User foreach (User u1 in user) 直接typeof(User)不就好了。[/quote] 学艺不精,见笑了 重新改了

Type t = typeof(User);

PropertyInfo[] propInfos = t.GetProperties();

List<User> user = new List<User>();

foreach (User u1 in user)
{
    foreach (var pi in propInfos)
    {
        string name = pi.Name;
        object value = pi.GetValue(u1, null);
    }
}
threenewbee 2013-05-21
  • 打赏
  • 举报
回复
引用 5 楼 oreoconansisu 的回复:
foreach (User u1 in user)
            {
                Type t = u1.GetType();

                foreach (var prop in t.GetProperties())
                {
                    string name = prop.Name;
                }
            }
傻不傻 如果你知道是User foreach (User u1 in user) 直接typeof(User)不就好了。
oreoconansisu 2013-05-21
  • 打赏
  • 举报
回复
foreach (User u1 in user)
            {
                Type t = u1.GetType();

                foreach (var prop in t.GetProperties())
                {
                    string name = prop.Name;
                }
            }
threenewbee 2013-05-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class User
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string EMail { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            object list = new List<User>();
            var properties = list.GetType().GetGenericArguments()[0].GetProperties();
            foreach (var item in properties)
                Console.WriteLine(item);
        }
    }
}
System.String Name Int32 Age System.String EMail Press any key to continue . . .
cym30 2013-05-21
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
到底是获取user对象的属性还是user对象(user对象是一个列表)中一个元素(User类型)的属性?
User类型的属性
threenewbee 2013-05-21
  • 打赏
  • 举报
回复
到底是获取user对象的属性还是user对象(user对象是一个列表)中一个元素(User类型)的属性?

110,567

社区成员

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

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

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