1, new test DLL and method, with vs2010 new WinForm program, the specific code is as follows:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
namespace reflect
{public
partial class Form1:form
{public
Form1 ()
{ InitializeComponent ();
}
public string Testreflect ()
{
MessageBox.Show ("Dynamically loading DLL Test");
Return "Testreflect returned value";}}
2. Dynamic Loading code
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Reflection;
namespace Reflecttest {public partial class Form1:form {public Form1 () {
InitializeComponent (); } private void Button1_Click (object sender, EventArgs e) {//Load DLL functions//a ssembly asm = assembly.load (Strdllpath)//dll Assembly ASM = assembly.loadfile (@ "F:\WorkSpace\VS test code for the current root directory)
\ Reflection Test 001\ Reflection Message Method \reflect\reflect\bin\debug\reflect.dll ")//the type's namespace and class are loaded//using the actual path of the DLL file to get the types System.Type fromclass = asm. GetType ("reflect.")
Form1 ");
You need to instantiate the type before you can use it, the parameters can be artificially specified, or no parameters can be used, and static instances can omit Object obj = System.Activator.CreateInstance (fromclass); Get a method by method name (debugging goes to the next step, you can eject the "Dynamic load DLL test" message.MethodInfo method = Fromclass.getmethod ("Testreflect"); Gets the return value of the Testreflect function, where the "Testreflect return value" is obtained, and if there is no return value, this step object o = Method can be omitted.
Invoke (obj, new object[] {}); }
}
}
Note
Methods in the method name to obtain the method must be public!
This paper url:http://www.bianceng.cn/programming/csharp/201410/45595.htm