可能重複:
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