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("动态加载Dll测试"); return "TestReflect返回值"; } } }
2、动态加载代码
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) { //加载dll中的函数 //Assembly asm = Assembly.Load(strDllPath);//加载当前根目录的dll Assembly asm = Assembly.LoadFile(@"F:\WorkSpace\VS测试代码\反射测试001\反射message要领\reflect\reflect\bin\Debug\reflect.dll");//依据dll文件现实途径加载 //用范例的定名空间和类取得范例 System.Type FromClass = asm.GetType("reflect.Form1"); //须要实例化范例,才能够运用,参数能够工资的指定,也能够无参数,静态实例能够省略 Object obj = System.Activator.CreateInstance(FromClass); //经由过程要领称号取得要领(调试走到下面这一步的时刻,就能够弹出“动态加载Dll测试”这个音讯了) MethodInfo method = FromClass.GetMethod("TestReflect"); //猎取TestReflect函数的返回值,在这里会猎取到"TestReflect返回值",假如没有返回值,能够省略这一步 object o = method.Invoke(obj, new object[] { }); } } }
小注:
经由过程要领称号取得要领中的要领必需是public的!
以上就是C# 动态加载Dll的内容,更多相关内容请关注ki4网(www.ki4.cn)!