How to instantiate List but T is unknown until runtime?

后端 未结 4 1201
一整个雨季
一整个雨季 2020-12-31 10:05

Assume I have a class that is unknown until runtime. At runtime I get a reference, x, of type Type referencing to Foo.GetType(). Only by using x and List<>, can I create

4条回答
  •  情歌与酒
    2020-12-31 10:51

    Given a type, you can instantiate a new instance this way:

    var obj = Activator.CreateInstance(type);
    

    Ref: http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx

提交回复
热议问题