Start this article: there are several ways to load an assembly into an application domain in the. NET framework. Each method uses a different class.
You can use the following overloaded method to load an assembly into an application domain:
The System.AppDomain class contains several overloaded load methods. Although these methods can be used to successfully load any assembly into the current or new application domain, they are primarily used for COM interop. You can also load an assembly using the CreateInstance method.
The System.Reflection.Assembly class consists of two static overloaded methods: Load and LoadFrom. Both of these methods vary depending on the context in which the load is loaded.
Simple example: a way to invoke another. exe file in an. 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 compilation succeeded. For Dy_loadasse.exe, perform the display:
Test DY Load Assembly
Placed in the same directory as the loadexe.exe generated below.
Document II:
Using System;
Using System.Reflection;
Namespace use_dy_load_assembly
{
Class Loadexe
{
[STAThread]
static void Main (string[] args)
{
Use the "file name to load" assembly into the current application domain.
Assembly a = Assembly.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"); It's always a mistake to write.
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 Adnamemethod method.
MyMethod. Invoke (Obj,null);
Execution result is test DY load assembly
The following is a call to the main method in Dy_loadasse.exe with an error
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 Adnamemethod method.
MyMethod. Invoke (Obj,null);
Console.ReadLine ();
}
}
}
In fact, whether you are an EXE or a DLL assembly can be loaded
Custom Bindings
In addition to being implicitly used by the compiler for late binding (referring to virtual methods, interfaces, and other related implementations), reflection can also be used explicitly in code to complete late binding. The common language runtime supports multiple programming languages, but these languages have different binding rules. In the case of early binding, the code generator can fully control this binding. However, when a late binding is performed through reflection, the binding must be controlled with a custom binding. The Binder class provides custom control over member selection and invocation.
With a custom binding, you can load an assembly at run time, get information about the type in that assembly, and then call a method on that type or access a field or property of that type. You can use this method if you do not know the type of the object at compile time, such as when the object type is dependent on user input.
Using System;
Namespace Dy_loaddll
{
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 + "" + txt);
}
}
}
Edit the build Dy_loaddll.dll, with LoadExe.exe under 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" assembly into the current application domain.
Assembly a = Assembly.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 a mistake, because the above DLL adds a 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 question if the method overloads, if it is a property. And what should I do?
/Check the relevant MSDN,
Ms-help://ms. Vscc/ms. Msdnvs.2052/cpguide/html/cpcondynamicallyloadingusingtypes.htm#cpcondynamicallyloadingusingtypes
Ms-help://ms. Vscc/ms. Msdnvs.2052/cpref/html/frlrfsystemtypeclassinvokemembertopic.htm