c# 動(dòng)態(tài)加載dll文件,并實(shí)現(xiàn)調(diào)用其中的簡(jiǎn)單方法
以下是測(cè)試代碼:
新建一個(gè)classlibrary,包含兩個(gè)類class1和class2,這兩個(gè)類中分別有一個(gè)方法,都是返回一個(gè)字符串,代碼如下:
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!"; } } }
在編譯完成后會(huì)生成一個(gè)mydll.dll動(dòng)態(tài)鏈接庫(kù),然后新建一個(gè)winform項(xiàng)目(其他也可以,調(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)部的類,可以“命名空間.父類……類名”;如果是外部的,需要在后面加上“,鏈接庫(kù)名”; //再次感謝thy38的幫助。 //Type ty = Type.GetType(t_class);//這兒在調(diào)試的時(shí)候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[] { });//這里是獲取一個(gè)類似于類的實(shí)例的東東 //object magicClassObject = Activator.CreateInstance(t);//獲取無參數(shù)的構(gòu)造實(shí)例還可以通過這樣 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ù)時(shí),這兒就不能這樣子啦 object magicClassObject = magicConstructor.Invoke(new object[] { }); MethodInfo mi = ty.GetMethod("saybeautiful"); object aa = mi.Invoke(magicClassObject, null);//方法有參數(shù)時(shí),需要把null替換為參數(shù)的集合 MessageBox.Show(aa.ToString()); } } //AppDomain pluginDomain = (pluginInstanceContainer[key] as PluginEntity).PluginDomain; //if (pluginDomain != null) //{ // AppDomain.Unload(pluginDomain); // } }
以上這篇c# 動(dòng)態(tài)加載dll文件,并實(shí)現(xiàn)調(diào)用其中的簡(jiǎn)單方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- C# WPF如何反射加載Geometry幾何圖形數(shù)據(jù)圖標(biāo)
- c# WPF中自定義加載時(shí)實(shí)現(xiàn)帶動(dòng)畫效果的Form和FormItem
- c# 實(shí)現(xiàn)網(wǎng)頁(yè)加載后將頁(yè)面截取為長(zhǎng)圖片
- C# 根據(jù)表格偶數(shù)、奇數(shù)加載不同顏色
- C# 動(dòng)態(tài)加載程序集信息
- C#中調(diào)用DLL時(shí)未能加載文件或程序集錯(cuò)誤的處理方法(詳解)
- C#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法
- C#使用Jquery zTree實(shí)現(xiàn)樹狀結(jié)構(gòu)顯示 異步數(shù)據(jù)加載
- C#使用反射加載多個(gè)程序集的實(shí)現(xiàn)方法
- C#實(shí)現(xiàn)動(dòng)態(tài)加載dll的方法
- c#動(dòng)態(tài)加載卸載DLL的方法
- 3種C# 加載Word的方法
相關(guān)文章
C#使用正則表達(dá)式實(shí)現(xiàn)漢字轉(zhuǎn)拼音
這篇文章主要為大家詳細(xì)介紹了C#如何使用正則表達(dá)式實(shí)現(xiàn)漢字轉(zhuǎn)拼音的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01兩路歸并的數(shù)組與鏈表的實(shí)現(xiàn)方法
本篇文章對(duì)兩路歸并的數(shù)組與鏈表的實(shí)現(xiàn)方法進(jìn)行了分析介紹。需要的朋友參考下2013-05-05List轉(zhuǎn)換成DataSet實(shí)現(xiàn)代碼
怎樣把List轉(zhuǎn)換成DataSet本人很是疑惑,于是搜集整理一番,需要的朋友可以參考下2012-12-12C#通過正則表達(dá)式實(shí)現(xiàn)提取網(wǎng)頁(yè)中的圖片
本文給大家分享的是使用C#通過正則表達(dá)式來實(shí)現(xiàn)提取網(wǎng)頁(yè)中的圖片的代碼,十分的方便,有需要的小伙伴可以參考下。2015-12-12C#使用DirectX.DirectSound播放語(yǔ)音
這篇文章主要為大家詳細(xì)介紹了C#使用DirectX.DirectSound播放語(yǔ)音,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03