您可以通过多种方式即时创建某种类型的对象,一种是:
// determine type herevar type = typeof(MyClass);// create an object of the typevar obj = (MyClass)Activator.CreateInstance(type);
然后您将在obj中获得MyClass的实例。
另一种方法是使用反射:
// get type informationvar type = typeof(MyClass);// get public constructorsvar ctors = type.GetConstructors(BindingFlags.Public);// invoke the first public constructor with no parameters.var obj = ctors[0].Invoke(new object[] { });从返回的ConstructorInfo之一中,您可以使用参数对其进行“ Invoke()”,并获得该类的实例,就好像您使用的是“ new”运算符一样。



