求教各位大侠。
反射遍历一个class,
已知class的一个成员是IList的,但是不知道具体是哪个泛型参数类型,
也就是<T>是未知的。
现在想要把他的值给取出来,或者能够强制转换object也可以。
这个该怎么做,忘各位大侠热心告知小弟一番。

解决方案 »

  1.   

    List<T> lis=new List<T>
    foreach(object obj in lis)
    {
           obj...
    }
      

  2.   

    GetType()
    和IS运算符不知道能解决不.
    没用过.
      

  3.   

    这几天一直忙着开会,现在大家热心的回答。可能一楼没有明白我的意思,我是要反射一个类,取他里面的值,
    这个类里面有个list。其中一部分代码:
    else if (info.PropertyType.Name.StartsWith("IList"))
    {
        object lis = info.GetValue(b, null);
        //TODO 这个lis取值要怎么取
    }
      

  4.   

    Type type = msgobject.GetType();
    if(type=="你的类类型")
    {
    (类) newclass=(类)msgobject;
    }
    newclass.列表名
      

  5.   

    尚无答案。贴一个好了.List<int> test = new List<int>();
    string type_full_name = test.GetType().GetGenericArguments()[0].FullName;//System.Int32类型
      

  6.   


    反射取出list<T>的System.Int32类型后,要怎么去list的值呢?
    能告知一下么?,谢谢
      

  7.   

    IList 实现了IEnumerator 接口,所以你可以反射调用GetEnumerator()方法,然后遍历。例:IList<int> A = new List<int>();
                A.Add(1);
                System.Collections.IEnumerator T = A.GetEnumerator();
                
                while(T.MoveNext())
                {
                    Console.WriteLine(T.Current);
                }
      

  8.   

    List<int> test = new List<int>();
    test.Add(50);
    string type_full_name = test.GetType().GetGenericArguments()[0].FullName;//System.Int32类型
    Console.WriteLine(test.GetType().GetMethod("get_Item").Invoke(test, new object[] { 0 }));//取数据
      

  9.   

    谢谢各位!
    特别感谢 wuyazhe 和 cdglynn 你们的正确指导。
    前面一直卡在list的item怎么取上,大多数人都通过list[n]来取。
    并且msdn上标明的list方法列表里面,也没有说明有什么具体的方法来去item。
    因此不知道怎么反射去item。最后我采用了 wuyazhe 的方法。主要是代码:
    test.GetType().GetProperty("Count").GetValue(test, null);//取list 的item的个数。
    test.GetType().GetMethod("get_Item").Invoke(test, new object[] { 0 });//取item