Class.forName()
-
In Java, you can create an instance of an object by name by getting the Class object by name, and then calling createInstance() - or something like that. Is this possible in C#? It looks like I might be able to do it through assemblies, but I'm not sure. Application: I want to be able to provide a configuration file that lists the class names of add-ins to load. The app would loop through these names, create instances and use them. I know I can do it with COM, but I'd rather not subject the authors of add-ins to writing COM servers. Anyways, is this possible? J
-
In Java, you can create an instance of an object by name by getting the Class object by name, and then calling createInstance() - or something like that. Is this possible in C#? It looks like I might be able to do it through assemblies, but I'm not sure. Application: I want to be able to provide a configuration file that lists the class names of add-ins to load. The app would loop through these names, create instances and use them. I know I can do it with COM, but I'd rather not subject the authors of add-ins to writing COM servers. Anyways, is this possible? J
You can use the
Assembly
class to load assemblies at runtime, theGetType
function to get an object's type, and theActivator.CreateInstance
static function to create an object of that type:Assembly a = Assembly.Load("MyLibrary");
Type t = a.GetType("MyLibrary.MyClass");
object obj = Activator.CreateInstance(t);cheers, Chris Maunder (CodeProject)