有一个方法A返回一个List<T>对象,在另一个方法B中接收,方法B中不能直接得到List<T>中T的类型,但却得遍历这个List<T>对象
问题:
能不能在B中通过反射获得T的对象类型?据说这是不可以的,因为在编绎期间T只作为一个占位符,在运行期间由具体类型替换。但如果这样的话,怎么遍历这个返回的List<T>对象?自带的DropDownList的DataSource可以为设定为List<T>,它这个应该也是遍历这个数据源,然后绑定的吧。

解决方案 »

  1.   

    我的目的扩展dropdownlist显示一颗树结构,先看dropdownlist的绑定方法:
    dropdownlist.DataSource = List<T>;
    dropdownlist.DataBind();我也想用这种方式:customContorl.DataSource = List<T>;
    然后在CustomContorl里遍历这个List<T>:
           protected override void Render(HtmlTextWriter writer)
           {
               .....
               List<ITree> listTree = (List<ITree>)dataSource;  
               //由于不能确定T的类型,我写了一个ITree的接口。这样一来,
               //每次我用这个自定义控件时T的类型都必须继承这个接口,这显然太2了
               //我想实现的效果正如自带的dropdownlist那样,只需知道它的DataSource是个List<T>类型就可以了
               WriteDropDownTree(listTree, -1);
               .....
           }
           private void WriteDropDownTree<T>(List<T> listTree, int rootID) where T : ITree
           {
                CreateTree(listTree, rootID);
           }        private void CreateTree<T>(List<T> listTree, int rootID) where T : ITree
            {
                foreach (T node in GetChildeNodes(listTree, rootID))
                {
                    sbTree.AppendFormat("<option value=\"{0}\">{1}</option>", node.ID, "..." + node.Name);
                    CreateSubOption(listTree,node.ID);
                }
            }
      

  2.   

    楼主给你举个例子希望对你有帮助
     protected void Page_Load(object sender, EventArgs e)
            {            List<string> list=AddItem();
                ShowItem(list);
            }        private void ShowItem(List<string> list)
            {
                foreach (string s in list)
                {
                    Response.Write(s+"<br/>");
                }
            }        private List<string> AddItem()
            {
                List<string> list = new List<string>();
                list.Add("1");
                list.Add("2");
                list.Add("3");
                list.Add("4");
                return list;
            }
      

  3.   

    @chen_ya_ping
    谢谢你的回答,现在的问题不是怎么遍历一个具体类型的List<T>,而是遍历未知具体类型的List<T>的问题
      

  4.   

    看了半天结合了楼主的代码才理解楼主的想要的效果,理解能力下降了,唉....楼主随便写一个定义索引器,然后绑定到DataSource就会发现系统会提示你绑定的索引器必须实现IEnumerable接口。所以,object里的List<T>可以转成IEnumerable,然后用foreach来遍历,关于T,就可以用反射来处理了。提一下,datalist之类的绑定数据的<%# Eval("") %>之类的就是通过反射来处理的。
      

  5.   

    @oyiboy
    关于T,就可以用反射来处理了。 
    但是这个T是在运行时动态替换的,这个也可以通过反射得到它类型么?我在网上找了好久资料都没有找到方法,能不能再具体一点?
      

  6.   


    foreach(T t in listTree)
    {
    Type itemType = t.GetType();
    string s = itemType.GetGenericTypeDefinition().FullName;
    }
      

  7.   

     List<string> a = new List<string>();
            a[0].GetType();
      

  8.   

        private void objListForEach(object collection)
        {
            IEnumerable enumerable = collection as IEnumerable;
            if (enumerable == null) { throw new Exception("collection必须实现IEnumerable接口"); }
            foreach (object item in enumerable)
            {
                Type t = item.GetType();
                /*这个地方你喜欢作什么就作什么啦*/
            }
        }    //或是你干脆可以这样
        private void objListForEach(IEnumerable collection)
        {
            foreach (object item in collection)
            {
                Type t = item.GetType();
                /*这个地方你喜欢作什么就作什么啦*/
            }
        }
    两段小代码,不知道是不是你想要的答案。
      

  9.   

    class Sample
    {
      void SomeMethodCore<T>(List<T> list)
      {
        // 这个地方你喜欢作什么就作什么,并且知道T的具体类型
        foreach(T item in list)
        {
           Console.WriteLine(item);
        }
      }  // 使用时,调用这个方法
      void SomeMethod(object itShouldBeAList)
      {
        if (itShouldBeAList == null)
          throw new ArgumentNullException("itShouldBeAList");
        if (!itShouldBeAList.GetType().IsGenericType)
          throw new ArgumentException("itShouldBeAList is not a List<T>");
        if (itShouldBeAList.GetType().GetGenericTypeDefinition() != typeof(List<>))
          throw new ArgumentException("itShouldBeAList is not a List<T>");
        typeof(Sample).GetMethod("SomeMethodCore", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(itShouldBeAList.GetType().GetGenericArguments()).Invoke(this, new object[] { itShouldBeAList });
      }  // 一个模拟的调用
      void Caller()
      {
        object list = new List<int>{1,2,3};
        SomeMethod(list);
      }
    }
      

  10.   

    @oyiboy
    看了你的代码之后才发现自己犯了一个很SB的错误,我一直想直接获取List<T>里T的类型,却忘了传说中那句 Object类是所有类的基类再说一次谢谢你,愿上帝与你同在
      

  11.   

    @vwxyzh
    你的代码刚好是我下一步需要做的,非常感谢
      

  12.   

    遍历未知具体类型那你不用foreach来遍历不就行了嘛,你用FOR循环就不用考虑类型了啊
      

  13.   

    不知道 object 能不能完成你所需要的操作。
      

  14.   

    list<T> 其中的t可以表示任意字符类型,就看自己定义了
      

  15.   

    都实例化了当然可以直接GetType,如上面,最好用IEnumerable迭代去做,foreach方便啊