In many cases, you need to create an instance based on the class information. The example of creating an instance using a constructor with parameters is as follows:
String assemblyfile;
String implclass;
String [] impctorsig; // an array of constructor parameter types, such as "system. String"
Protected object createinstance (object [] ctorargs)
{
// Load assembly
Assembly ass = assembly. loadfrom (assemblyfile );
// Get class, which must be a class name with a namespace
Type T = ass. GetType (implclass );
// The parameter type of the constructor
Type [] ctorsigarr = type. gettypearray (implctorsig );
// Obtain the constructor Information
Constructorinfo ctorinfo = T. getconstructor (ctorsigarr );
// Construct an instance through the parameter list
Return ctorinfo. Invoke (ctorargs );
}
The common methods to create object instances without constructors are as follows:
Object o = activator. createinstance (t );