How to dynamically load DLLs in C #

Source: Internet
Author: User
Tags reflection root directory

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

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.