Official starts this article: In the .NET framework, there are several ways to load the assembly to the application domain. Each method uses a different class.
You can load the assembly to the application domain using the overloaded method below:
The System.AppDomain includes several overloaded LOAD methods. Although these methods can be used to successfully load any assembly to the current or new application domain, but they are mainly used for COM interaction. You can also load an assembly using the CreateInstance method.
The System.Reflection.Assembly class contains two static overload methods: LOAD and LOADFROM. These two methods varies depending on the load context.
Simple Example: Method of calling another .exe file in a .exe file.
Using system;
Namespace DY_LOADASSE
{
Class testclass
{
[Stathread]
Static void main (string [] args)
{
Outuse test = new outuse ();
TEST.OUTPUT ();
Console.readline ();
}
}
Class Outuse
{
Public outuse ()
{
}
Public void output ()
{
Console.writeline ("Test Dy Load Assembly");
}
}
}
The above compiled success. Perform display for DY_LOADASSE.EXE:
Test Dy Load Assembly
Put it in the same directory with Loadexe.exe that is produced below.
Document 2:
Using system;
Using system.reflection;
Namespace USE_DY_LOAD_ASSEMBLY
{
Class Loadexe
{
[Stathread]
Static void main (string [] args)
{
// use the file name to load the assembly in the capital application domain.
AskEMBLY A = askMBLY.LOADFROM ("DY_LOADASSE.EXE");
TYPE [] TYPES2 = a.gettypes ();
Foreach (Type T in Types2)
{
Console.writeline (T.Fullname);
}
// Get the Type to use.
// Type mytype = a.gettype ("outuse"); this is always wrong
TYPE mytype = a.gettype ("DY_LOADASSE.outuse);
Console.writeline (mytype.fullname);
// Get The Method to Call.
MethodInfo mymethod = mytype.getMethod ("OUTPUT");
/// CREATE AN INSTANCE.
Object obj = activator.createInstance (MyTYPE);
/// // execute the adnamethod method.
Mymethod.invoke (OBJ, NULL);
// Execute result is Test Dy Load Assembly
/ / The following is the main method in calling DY_LOADASSE.EXE, an error occurs
// Type mytype = a.gettype ("DY_LOADASSE.TESTCLASS");
// console.writeline (mytype.fullname);
/// Get The Method to Call.
//MethodInfo mymethod = mytype.getMethod ("main");
/// CREATE AN INSTANCE.
// Object obj = activator.createInstance (MyType);
/// // execute the adnamethod method.
// mymethod.invoke (OBJ, NULL);
Console.readline ();
}
}
}
In fact, no matter how you are .EXE or DLL, you can be loaded.
Custom binding
In addition to implicitly used by the compiler to perform advanced bindings (referring to the Binding of the Virtual method, the interface, etc.), the reflection can also be explicitly used in the code to complete the late binding. The public language runtime supports multiple programming languages, but the binding rules of these languages are different. In the case of early binding, the code generator can fully control this binding. However, when the late binding is performed by reflection, the binding must be controlled with a custom binding. The Binder class provides custom control for members selection and calling.
With custom bindings, you can load an assembly at runtime to get information about the type of the program, and then call the method or to access the field or attribute of this type. This method can be used if you don't know the type of object when you compile (for example, when the object type depends on user input).
Using system;
Namespace DY_LOADLL
{
Public class helloworld
{
// constant hello world string.
Private const string m_helloWorld = "Hello World";
// Default public constructor.
Public HelloWorld ()
{
}
// Print "Hello World" Plus the Passed Text.
Public void printhello (String txt)
{
// output to the console.
Console.writeLine (M_HelloWorld " TX);
}
}
}
Editing Generates DY_LOADDLL.DLL, following Loadexe.exe below the same directory.
Using system;
Using system.reflection;
Namespace USE_DY_LOAD_ASSEMBLY
{
Class Loadexe
{
[Stathread]
Static void main (string [] args)
{
// use the file name to load the assembly in the capital application domain.
AskEMBLY A = askMBLY.LOADFROM ("DY_LOADDLL.DLL");
TYPE [] TYPES2 = a.gettypes ();
Foreach (Type T in Types2)
{
Console.writeline (T.Fullname);
}
// get the type to use.// type mytype = a.gettype (""); this is always wrong because the above DLL added namespace
TYPE mytype = a.gettype ("DY_LOADDLL.HELLOWORLD");
// Type mytype = a.gettype ("HelloWorld");
Console.writeline (mytype.fullname);
/// Get The Method to Call.
MethodInfo PrintMethod = mytype.getMethod ("Printhello");
// Create An Instance of The HelloWorld Class.
Object obj = activator.createInstance (MyTYPE);
// CREATE The args array.
Object [] test = new object [1];
// set the arguments.
TEST [0] = "from csharp late bound";
// Invoke the printhello method.
PrintMethod.invoke (OBJ, TEST);
Console.readline ();
}
}
}
Of course, we can't help but problem, if the method is overloaded, if it is attribute. What should I do? ?
/ Check the relevant MSDN,
Ms-help: //ms.vscc/ms.msdnvs.2052/cpguide/html/cpcondynamicallyloadingysingtypes.htm#cpcondynamicallyLoadingUsingysingTypes
MS-help: //ms.vscc/ms.msdnvs.2052/cpref/html/frlrfsystemtypeclassinvokememembertopic.htm