First, we need to encapsulate a dll,vs2008 to create a class library with the following code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;using usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacedll{ Public classAddClass { Public StaticInt32 Add (Int32 I, Int32 j) {returni +J; } }}
Compile the build Dll.dll, where the class name is AddClass, and the method is Add.
Next call this Dll.dll in the main program, you need to first copy this dll.dll to the main program's Bin\Debug folder. A using System.Reflection is required for dynamic reference DLLs; This reflects the namespace.
Private voidTest () {Assembly= Assembly.Load ("DLL");//Loading DLL filesType TP =. GetType ("Dll.addclass");//get class name, Namespace + class name requiredObject obj = activator.createinstance (TP);//Create an instanceMethodInfo meth = TP. GetMethod ("Add");//Get Method intt = Convert.ToInt32 (meth. Invoke (obj,Newobject[]{2,3}) );//invoke Invoke MethodMessageBox.Show (t.tostring ()); }
Described above is the method of dynamically calling DLLs, you can also use the reference---add Reference (Dll.dll) method to pre-load, and with the using DLL namespace name; To use. The main program is directly using int t= Addclass.add (2, 3); On the line.
System.Text; namespace dll{ publicclass addclass { publicstatic Int32 Add (Int32 I, Int32 j) { return i + j;}} }
C # Dynamic State Reference DLL method