C # Dynamic State Reference DLL method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.