C # dynamically loading DLLs

Source: Internet
Author: User
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)!

  • Related Article

    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.