可能重复:
Activator.CreateInstance - How to create instances of classes that have parameterized constructors非默认构造函数
我想知道如何不使用默认的构造函数在运行时创建确定的类型的对象。
也就是说,我有BaseClass
和各种子类。
Type type; //this variable will be one of the child classes
BaseClass base = Activator.CreateInstance(type);
这允许我创建一个具有默认构造函数的子类对象,但我想调用一个特定的构造函数。我知道所有的子类都有一个构造函数需要一定的参数,所以我不担心那个构造函数不存在。我发现this的问题,但最好的我可以到那里是一个单一的字符串参数。这是可行的吗?
http://stackoverflow.com/questions/1288310/activator-createinstance-how-to-创建-实例-的类 - 即具备的,paramete#答案-1288333 – xandercoded