object obj = Assembly.Load("my.DAL").CreateInstance("my.DAL." + str);//得到该类型的实例
list<?> mylist=new list<?>如何创建List呢

解决方案 »

  1.   

    http://blog.csdn.net/ojlovecd/article/details/2943968
      

  2.   

    var listType = typeof(List<>).MakeGenericType(obj.GetType());
    var list = Activator.CreateInstance(listType);不过Add什么的还要反射。
      

  3.   

    楼上的很全面了.想直接看到效果,运行下面的代码即可
    命令控制台程序
        class Program
        {
            static void Main(string[] args)
            {
                Type tempListType = typeof(List<>);
                Type[] tempGenericTypeArgs = new Type[] { typeof(Student)};
                tempListType = tempListType.MakeGenericType(tempGenericTypeArgs);
                Object tempList = Activator.CreateInstance(tempListType);
                Console.WriteLine(tempList.GetType().ToString());
            }
        }
        public class Student
        {
            public String Name { get; set; }
        }
      

  4.   

    转换成object使用的时候再换回来
      

  5.   


    学习了,随便问个问题 就是我foreach的时候
    foreach(var item list)
    {
        通常item的智能提示就是object的智能提示,请就纠结
    }
      

  6.   

    把foreach里面的var声明,改为具体的类型就可以了
      

  7.   

    C# code
    var listType = typeof(List<>).MakeGenericType(obj.GetType());
    dynamic list = Activator.CreateInstance(listType);
    list.Add(xxxxxx);
    Add的时候就不用反射了
      

  8.   

    既然不知道类型,我觉得可以使用object或者非泛型版本的ArrayList。
    如果下文还要那个调用那个obj的方法,直接声明为 dynamic obj将为你省下来许多代码。
    要求List内的对象类型单一的话,结合2楼的代码和dynamic声明:var listType = typeof(List<>).MakeGenericType(obj.GetType());
    dynamic list = Activator.CreateInstance(listType);
    list.Add(....)
      

  9.   


    public T GetClass<T>(string str)
    {
        object obj = Assembly.Load("my.DAL").CreateInstance("my.DAL." + str);
        return (T)obj;  
    }
      

  10.   

    public class MyList<T>
    {
    private List<T> m_lValues = new List<T>();public void Add(T t)
    {
    m_lValues.Add(t);
    }
    //..
    }object obj = Assembly.Load("my.DAL").CreateInstance("my.DAL." + str);//得到该类型的实例MyList<MyObject> mylist = new MyList<MyObject>();
    MyObject tmp = obj as MyObject; // 这里你应该是知道类型的吧。
    if (tmp != null)
    {
    mylist.Add(tmp);
    }