1#
转自:http://bbs.csdn.net/topics/360129652

List<T> 中的T能否动态生成(动态生成List<T>)
  1.    static void Main()
  2.         {
  3.             object intList = MakeList(typeof(int), 1, 2, 3);
  4.             object strList = MakeList(typeof(string), "sdfd", "fet");

  5.             //List<int>
  6.             foreach(object obj in (System.Collections.IEnumerable)intList)
  7.                 Console.WriteLine(obj);

  8.             //List<string>
  9.             foreach(object obj in (System.Collections.IEnumerable)strList)
  10.                 Console.WriteLine(obj);
  11.         }

  12.         static object MakeList(Type t, params object[] items)
  13.         {
  14.             Type type = typeof(List<>).MakeGenericType(t);

  15.             object list = Activator.CreateInstance(type);
  16.             System.Collections.IList ilist = list as System.Collections.IList;
  17.             foreach (object o in items)
  18.                 ilist.Add(o);
  19.             return list;
  20.         }

复制代码