From: http://blog.csdn.net/jaydawson/article/details/5539438
C # use the following method to dynamically create a class instance in a class factory:
1. activator. createinstance (type) 2. activator. createinstance (type, object []) |
|
The differences between the two methods are as follows: creating a constructor without parameters and creating a constructor with parameters.
// Activator. createinstance (type)
Object result = NULL;
Type typeofcontrol = NULL;
Typeofcontrol = type. GetType (vfullclassname );
Result = activator. createinstance (typeofcontrol );
// Activator. createinstance (type, object [])
Object result = NULL;
Type typeofcontrol = NULL;
Typeofcontrol = type. GetType (vfullclassname );
Result = activator. createinstance (typeofcontrol, objparam );
However, during dynamic creation, instances of classes in the DLL of an external application may be dynamically used. In this case, decompilation is required and the Assembly class under the reflection naming control is used.
// First load the DLL using the Assembly class, and then obtain the class based on the full path of the class
Object result = NULL;
Type typeofcontrol = NULL;
Assembly tempassembly;
Tempassembly = assembly. loadfrom (vdllname );
Typeofcontrol = tempassembly. GetType (vfullclassname );
Result = activator. createinstance (typeofcontrol, objparam );
C # activator. createinstance () method