c# 動態(tài)加載dll文件,并實現(xiàn)調(diào)用其中的簡單方法
更新時間:2017年01月26日 10:44:59 投稿:jingxian
下面小編就為大家?guī)硪黄猚# 動態(tài)加載dll文件,并實現(xiàn)調(diào)用其中的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
以下是測試代碼:
新建一個classlibrary,包含兩個類class1和class2,這兩個類中分別有一個方法,都是返回一個字符串,代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace mydll { public class Class1 { public Class1() { } public string sayhello() { return "hello,word!"; } } public class Class2 { public Class2() { } public string saybeautiful() { return "beautiful,very good!"; } } }
在編譯完成后會生成一個mydll.dll動態(tài)鏈接庫,然后新建一個winform項目(其他也可以,調(diào)試用):
private void button1_Click(object sender, EventArgs e) { string path = @"D:\123\mydll\mydll\bin\Debug\mydll.dll"; //Byte[] byte1 = System.IO.File.ReadAllBytes(path);//也是可以的 //Assembly assem = Assembly.Load(byte1); Assembly assem = Assembly.LoadFile(path); //string t_class = "mydll.Class1"; //理論上已經(jīng)加載了dll文件,可以通過命名空間加上類名獲取類的類型,這里應(yīng)該修改為如下: //string t_class = "mydll.Class1,mydll";//如果你想要得到的是被本工程內(nèi)部的類,可以“命名空間.父類……類名”;如果是外部的,需要在后面加上“,鏈接庫名”; //再次感謝thy38的幫助。 //Type ty = Type.GetType(t_class);//這兒在調(diào)試的時候ty=null,一直不理解,望有高人可以解惑 Type[] tys = assem.GetTypes();//只好得到所有的類型名,然后遍歷,通過類型名字來區(qū)別了 foreach (Type ty in tys)//huoquleiming { if (ty.Name == "Class1") { ConstructorInfo magicConstructor = ty.GetConstructor(Type.EmptyTypes);//獲取不帶參數(shù)的構(gòu)造函數(shù) object magicClassObject = magicConstructor.Invoke(new object[] { });//這里是獲取一個類似于類的實例的東東 //object magicClassObject = Activator.CreateInstance(t);//獲取無參數(shù)的構(gòu)造實例還可以通過這樣 MethodInfo mi = ty.GetMethod("sayhello"); object aa=mi.Invoke(magicClassObject, null); MessageBox.Show(aa.ToString());//這兒是執(zhí)行類class1的sayhello方法 } if (ty.Name == "Class2") { ConstructorInfo magicConstructor = ty.GetConstructor(Type.EmptyTypes);//獲取不帶參數(shù)的構(gòu)造函數(shù),如果有構(gòu)造函數(shù)且沒有不帶參數(shù)的構(gòu)造函數(shù)時,這兒就不能這樣子啦 object magicClassObject = magicConstructor.Invoke(new object[] { }); MethodInfo mi = ty.GetMethod("saybeautiful"); object aa = mi.Invoke(magicClassObject, null);//方法有參數(shù)時,需要把null替換為參數(shù)的集合 MessageBox.Show(aa.ToString()); } } //AppDomain pluginDomain = (pluginInstanceContainer[key] as PluginEntity).PluginDomain; //if (pluginDomain != null) //{ // AppDomain.Unload(pluginDomain); // } }
以上這篇c# 動態(tài)加載dll文件,并實現(xiàn)調(diào)用其中的簡單方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C# WPF如何反射加載Geometry幾何圖形數(shù)據(jù)圖標(biāo)
- c# WPF中自定義加載時實現(xiàn)帶動畫效果的Form和FormItem
- c# 實現(xiàn)網(wǎng)頁加載后將頁面截取為長圖片
- C# 根據(jù)表格偶數(shù)、奇數(shù)加載不同顏色
- C# 動態(tài)加載程序集信息
- C#中調(diào)用DLL時未能加載文件或程序集錯誤的處理方法(詳解)
- C#中加載dll并調(diào)用其函數(shù)的實現(xiàn)方法
- C#使用Jquery zTree實現(xiàn)樹狀結(jié)構(gòu)顯示 異步數(shù)據(jù)加載
- C#使用反射加載多個程序集的實現(xiàn)方法
- C#實現(xiàn)動態(tài)加載dll的方法
- c#動態(tài)加載卸載DLL的方法
- 3種C# 加載Word的方法
相關(guān)文章
C#使用正則表達(dá)式實現(xiàn)漢字轉(zhuǎn)拼音
這篇文章主要為大家詳細(xì)介紹了C#如何使用正則表達(dá)式實現(xiàn)漢字轉(zhuǎn)拼音的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01List轉(zhuǎn)換成DataSet實現(xiàn)代碼
怎樣把List轉(zhuǎn)換成DataSet本人很是疑惑,于是搜集整理一番,需要的朋友可以參考下2012-12-12C#通過正則表達(dá)式實現(xiàn)提取網(wǎng)頁中的圖片
本文給大家分享的是使用C#通過正則表達(dá)式來實現(xiàn)提取網(wǎng)頁中的圖片的代碼,十分的方便,有需要的小伙伴可以參考下。2015-12-12