C # Dynamically creates an instance of a class in a class factory using the following methods:
1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, object[]) |
The two methods differ only by creating a parameterless construction method and creating a constructor with parameters.
1, like the controller layer in the MVC framework: you need to dynamically create an instance of a controller:
Controller c=activator.createinstance (Commandmap (eventName)) as Controller;
2, Activator.CreateInstance (type,object[])
object result = NULL;
Type Typeofcontrol =null;
Typeofcontrol = Type.GetType (vfullclassname);
result = Activator.CreateInstance (Typeofcontrol, Objparam);
3.
However, when dynamically created, an instance of a class in an externally applied DLL might be used dynamically, and then an anti-compilation operation is required, using the Assembly class under the reflection named control.
Load the DLL with the Assembly class first, and then get 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);
The C # activator.createinstance () method uses