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 ("Dynamic load 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 function//assembly ASM = Assembly.Load (Strdllpath);//load DLL of current root directory Assembly ASM = assembly.loadfile (@ "F:\WorkSpace\VS test code \ Reflection Test 001\ reflection mess The Age Method \reflect\reflect\bin\debug\reflect.dll "),//load//with type namespace and class based on DLL file actual path to get type System.Type Fromclass = asm. GetType ("reflect. Form1 "); A static instance can omit Object obj = System.Activator.CreateInstance (fromclass) if it needs to instantiate a type, can be used, the parameter can be specified artificially, or it can have no parameters; Get the method by the method name (debugging go to the next step, you can pop up the "Dynamic load DLL Test" this message) MethodInfo method = Fromclass.getmethod ("Testreflect"); Get TThe return value of the Estreflect function, where it gets to the "Testreflect return value", if there is no return value, you can omit this step of object o = method. Invoke (obj, new object[] {}); } }}
Small bet
Methods that are obtained by means of method names must be public!
The above is the content of C # dynamic load DLL, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!