简体   繁体   中英

Creating an instance from a class name

I'm trying to create an instance of a class at run time. The classes I'm trying to create all inherit from a base class, ConfigMgrObj, and are named ConfigMgr_xxxxxx eg ConfigMgr_Collection . They all take a special object that I'm calling oController and a string as arguments.

This is the line I'm using to do it, where ClassToGet is a string that contains the name of the class eg ConfigMgr_Collection .

object oNewObject = System.Activator.CreateInstance(null, "StackOverflowNamespace." + ClassToGet, new object[] { oController, ClassToGet });

This throws a TypeLoadException exception. What's up with it?

Are the types you're trying to instantiate actually declared within the same assembly? Passing null as the first parameter is telling Activator that the types live in the current assembly.

看起来好像在当前程序"StackOverflowNamespace."+ClassToGet找不到名为"StackOverflowNamespace."+ClassToGet的类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM