Creating a instance of a type loaded from assembly
Filed in Uncategorized on Mar.08, 2008
Sometimes classes are seperated i different assemblies and you need to load classes from a specific assembly. Here is a way to load a class and instanciate a variable of that type:
try {
System.Runtime.Remoting.ObjectHandle objHandle =
System.Activator.CreateInstanceFrom(Application.StartupPath + @"\MyAssembly.exe", @"MyNameSpace." + @"MyClassName");
} catch(TypeLoadException) {
// Problems loading class
}
if(objHandle != null) {
MyClassName myObject = ((MyClassName) (objHandle.Unwrap()));
}
If you want to create an object instance of an object in the current assembly, simply use: System.Activator.CreateInstance() with the number of arguments needed for the types constructor.
Twitter