解决方案 »

  1.   

    到底是获取user对象的属性还是user对象(user对象是一个列表)中一个元素(User类型)的属性?
      

  2.   

    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 . . .
      

  3.   

    foreach (User u1 in user)
                {
                    Type t = u1.GetType();                foreach (var prop in t.GetProperties())
                    {
                        string name = prop.Name;
                    }
                }
      

  4.   

    傻不傻
    如果你知道是User
    foreach (User u1 in user)
    直接typeof(User)不就好了。
      

  5.   

    傻不傻
    如果你知道是User
    foreach (User u1 in user)
    直接typeof(User)不就好了。学艺不精,见笑了重新改了
    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);
        }
    }
      

  6.   

    傻不傻
    如果你知道是User
    foreach (User u1 in user)
    直接typeof(User)不就好了。学艺不精,见笑了重新改了
    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);
        }
    }
    呵呵,那还需要
    List<User> user = new List<User>();
    foreach (User u1 in user)
    么?
      

  7.   

    傻不傻
    如果你知道是User
    foreach (User u1 in user)
    直接typeof(User)不就好了。学艺不精,见笑了重新改了
    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);
        }
    }
    呵呵,那还需要
    List<User> user = new List<User>();
    foreach (User u1 in user)
    么?这个多写了一步,通过反射取属性值
      

  8.   


    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的名称和值。      
      

  9.   


    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的名称和值。
      

  10.   


    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的名称和值。      
    都给你代码了。
      

  11.   

    获得值只需要在我的代码上加上 item.GetValue(user) 即可。
      

  12.   

    你这样只能得到List里的第一个对象,List.Add(A),List.Add(B) 泛型中有2个数据对象的时候只能得到第一个。
      

  13.   

    你这样只能得到List里的第一个对象,List.Add(A),List.Add(B) 泛型中有2个数据对象的时候只能得到第一个。我是举例啊你可以用
    foreach (var obj in (list As IEnumerable))
        item.GetValue(obj, null);
      
      

  14.   

    就是一个获取getpropertys(),然后循环输出