1. Use the System. Activator class
System. activator provides two static methods: createinstance () and createinstancefrom (). If the Assembly containing the class is not found in Appdomain, call createinstance () and createinstancefrom () this will cause the assembly to be loaded.
Using System;
Using System. Reflection;
Namespace ConsoleApplication5
{
ClassProgram
{
Staticvoid Main (String [] args)
{
Assemblyassembly = Assembly. Load ("Foo. dll ");
Typetype = assembly. GetType ("NMFoo. Calc ");
Objectobj = Activator. CreateInstance (type );
}
}
}
2. Use System. Appdomain. Similar to the previous Activator. CreateInstance, but they can choose the Appdomain in which the object is created. In addition, the "AndUnwarp ()" version returns a direct reference to the object.
Using System;
Using System. Reflection;
Class Program {
Staticvoid Main (){
Objectobj = AppDomain. CurrentDomain. CreateInstanceAndUnwrap (
"Foo. dll", "NMFoo. Calc ");
}
}
3. Use the System. Reflecton. ConstructorInfo class. The invoke () method of this class creates a later binding for the constructor internally and calls the constructor through this binding.
Using System;
Using System. Reflection;
Namespace ConsoleApplication5
{
ClassProgram
{
Staticvoid Main (String [] args)
{
Assemblyassembly = Assembly. LoadFrom (@ "C: \ Foo. dll ");
Typetype = assembly. GetType ("NMFoo. Calc ");
ConstructorInfoconstructorInfo = type. GetConstructor (new Type [0]);
Objectobj = constructorInfo. Invoke (new object [0]);
}
}
}
4. Use the System. Type class
You can use the non-static method Invokemember () of the System. Type class to create an instance of an unknown class during compilation. You only need to use CreateInstance in the BIndingFlages Enumeration During the call.
Using System;
Using System. Reflection;
Namespace ConsoleApplication5
{
ClassProgram
{
Staticvoid Main (String [] args)
{
Assemblyassembly = Assembly. LoadFrom (@ "C: \ Foo. dll ");
Typetype = assembly. GetType ("NMFoo. Calc ");
Objectobj = type. InvokeMember (
Null, // Don't need toprovide a name for calling a constructor.
BindingFlags. CreateInstance,
Null, // Don't need abinder.
Null, // Don't need atarget object since we build it.
New Object [0]); // No parameters.
// Here, 'obj 'is a reference toward an instance of NMFoo. Calc.
}
}
}
5. Instances of arrays and delegate objects
To create an Array, you must call the static method Createinstance () in the System. Array class ()
To create a Delegate object, you must call the CreateDelegate () method in the System. Delegate class.
6. One binding and multiple calls
Using System;
Using System. Reflection;
Namespace ConsoleApplication5
{
ClassProgram
{
Staticvoid Main ()
{
Objectobj = AppDomain. CurrentDomain. CreateInstanceAndUnwrap (
"Foo. dll", "NMFoo. Calc ");
Typetype = obj. GetType ();
// Create a late bind with the 'sum '? Method.
MethodInfomethodInfo = type. GetMethod ("Sum ");
Object [] parameters = new object [2];
Parameters [0] = 7;
Parameters [1] = 8;
Intresult;
// 10 callto 'sums'
// For (int I = 0; I <10; I ++)
Result = (int) methodInfo. Invoke (obj, parameters );
Console. WriteLine (Convert. ToString (result ));
}
}
}
7. Using Interfaces: use the correct method for later binding
1. Code for creating an assembly containing interfaces (InterfaceAsm. CS)
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace NMFoo
{
Publicinterface ic1c
{
IntSum (int a, intb );
}
}
2. Code for creating the assembly of the target class (ClassAsm. CS)
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace NMFoo
{
Publicclass CalcWithInterface: ic1c
{
PublicCalcWithInterface ()
{
Console. WriteLine ("Calc. constructor called! ");
}
Publicint Sum (int a, int B)
{
Console. WriteLine ("method Calc. Sum () Called! ");
Returna + B;
}
}
}
3. The Code (ProgramAsm. CS) of the customer assembly whose target class is unknown during the compilation period)
Using System;
Using System. Reflection;
Using NMFoo;
Using System. IO;
Namespace Main
{
ClassProgram
{
Staticvoid Main (String [] args)
{
// Console. WriteLine (System. Environment. CurrentDirectory );
ICalcobj = AppDomain. CurrentDomain. CreateInstanceAndUnwrap ("ClassAsm. dll", "NMFoo. CalcWithInterface") as ic1c;
// Intresult = obj. Sum (7, 8 );
Console. WriteLine (System. Environment. CurrentDirectory );
}
}
}
4,