本帖最后由 marjay 于 2011-11-09 22:12:24 编辑

解决方案 »

  1.   

    static void GetModelList<T>(List<T> listT)
        {
            foreach (var p in listT)
            {        }
        }
      

  2.   


    那个。。俺用的2005 貌似var不出来?
      

  3.   

    你看到的都是VS反射出来的...基本上,如果需要大量反射操作就不应该用泛型,否则就是滥用泛型...另外var也只是个3.0的语法糖而已,不可能让你“点”出来什么,因为T仍然是未知类型...
      

  4.   

    什么都出不来吗?应该Object的几个函数还是可以用的吧。原因大家都说了,因为不知道T是什么类型。像你的代码static void GetModelList<T>(List<T> listT)
        {
            foreach (T p in listT)
            {
                 如果这里写
                 int i = p.Age;如果
                 GetModelList<int>就会有问题了。
            }
        }因此你的情况不适合使用泛型。
      

  5.   

    另外可以使用where。来限定类型
    例如public class MyGenericClass<T> where T : IComparable, new()
    {
        // The following line is not possible without new() constraint:
        T item = new T();
    }这样,就可以在函数里使用item.CompareTo(...)   也可以new了。参考
    where (generic type constraint) (C# Reference)
    http://msdn.microsoft.com/en-us/library/bb384067.aspx
      

  6.   

    按lz的写法,T只有在调用的才知道,那么你现在非调用的设计期,那自然不知道是啥类型一般两种方案1.如果你确定这个操作就是针对test类型滴,那么请使用泛型约束定义,这样你就可以xx.yy点出来2.如果你是针对任意类型,那么建议用action<T> 或者func<T> 委托出去,让调用者自己确定如何操作比如:
    static void GetModelList<T>(List<T> listT,Action<T> act)
        {
            foreach (var p in listT)
            {
                  act(p);
            }
        }GetModelList<TEST>(persons,p=>p.xxx=1);
      

  7.   

                        foreach (T p in listT)
                        {
                            foreach (PropertyInfo pinfo in typeof(T).GetProperties())
                            {
                                Console.Write(pinfo.Name+":"+pinfo.GetValue(p, null));
                                Console.WriteLine();
                            }                    } 获取值可以,但是你要返回对象就难了,所以你这个方法不靠谱