Creating an instance from a class name

I am trying to instantiate a class at runtime. The classes I'm trying to create inherit from the base class ConfigMgrObj and are called ConfigMgr_xxxxxx for example. ConfigMgr_Collection . They all take a special object, which I call oController and the string as arguments.

This is the string that I use for this, where ClassToGet is the string that contains the class name, for example. ConfigMgr_Collection .

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

This throws a TypeLoadException. What about him?

+2
source share
2 answers

Are the types you are trying to create actually declared in the same assembly? Passing null as the first parameter tells Activator that the types live in the current assembly.

+6
source

It looks like he did not find a class named "StackOverflowNamespace."+ClassToGet in the current assembly.

0
source

Source: https://habr.com/ru/post/1343439/


All Articles