C_Sharp 板


LINE

※ 引述《Syung1983 (小永)》之銘言: : 請問一下 應用程式可新增功能,類似plugin的方法如何實作? : 舉例來說有功能 A、B、C、D、E等模組,模組間互相獨立或相依 : 使用者如果需要A的功能,則安裝A模組就好。 : 如果需要C跟D的功能,則安裝這兩個模組就可以給另一位使用者 : 請問網路上有在討論此種作法的網站嗎? : 目前我從網路上找到的資料只有 : Michael Tsai大所提到的: 動態型別應用例:動態載入 DLL模組 : 請問一下如果要搜尋的關鍵字應該使用什麼 要做 plug-in,首先你要搞清楚有什麼 function 和 proerty 甚至是 delegate 是需要共用的 然後你必需把它們訂成 interface,並且實作它編成 class library dll 舉個我以前有個做到一半的網路相簿下載器當例子: ( 真的有興趣的,完整的 source code 在 SourceForge 上 http://t-photo-ripper.svn.sourceforge.net/viewvc/t-photo-ripper/ Code 應該是可以 build,不過上面的 plug-in 應該都因為太久沒更新而沒用了 ) 首先我訂出了一個 IPhotoRipperPlugIn 的 interface 這是所有的下載 plug-in 都必需要實作的 function 以及 property public interface IPhotoRipperPlugIn { string PlugInName { get; } string Author { get; } string TargetSiteURL { get; } URLType GetURLType(string aURL); AlbumList GetAlbumList(string aURL, string aReferer); AlbumList GetAlbumList(string aURL, string aReferer, string aUsername, string aPassword); AlbumPicList GetAlbumPicList(string aURL, string aReferer); AlbumPicList GetAlbumPicList(string aURL, string aReferer, string aUsername, string aPassword); string GetLoginedCookie(string aUsername, string aPassword); } 舉一個簡單的例子來說,每一個 PlugInName 都必需要實作 TargetSiteURL property 這樣才能透過問每個 plugin 的 TargetSiteURL,來得知 plugin 是針對哪個網站的 接下來是實作: // PlugIn.cs (1) namespace TPhotoRipperPlugIn { public class PlugIn : IPhotoRipperPlugIn { public string PlugInName { get { return "KTXP"; } } public string TargetSiteURL { get { return "http://comic.ktxp.com/"; } } } } // PlugIn.cs (2) namespace TPhotoRipperPlugIn { public class PlugIn : IPhotoRipperPlugIn { public string PlugInName { get { return "DMZJ"; } } public string TargetSiteURL { get { return "http://comic.dmzj.com/"; } } } } 像上面的情形你問 PlugIn (1) 它的 TargetSiteURL 就會是 http://comic.ktxp.com/ PlugIn (2) 就會回答你 http://comic.dmzj.com/ 這當然只是節錄,真的 class 要把整個 interface 的東西都實作完 這兩個 class 分屬兩個 plug-in,為了方便載入起見,兩個 plug-in 的實作 class 都叫做 PlugIn,並且都位於 TPhotoRipperPlug namesapce 下 接下來是實際載入的部分 public class TPhotoRipperPlugInLoader { private Assembly _asmb; private Object _obj; private IPhotoRipperPlugIn _plugIn; private string _lastError; public TPhotoRipperPlugInLoader(string aPlugInPath) { if (!File.Exists(aPlugInPath)) { _lastError = "DLL file not exists."; return; } try { // 由 Assembly.LoadFrom 載入 dll 檔案 // 並產生 Assembly _asmb = Assembly.LoadFrom(aPlugInPath); if (_asmb == null) { _lastError = "Load PlugIn assembly failed"; return; } //由 CreateInstance 建立一個宣告在 //TPhotoRipperPlugIn namespace 下 //名字叫做 PlugIn 的 class 的實體物件 _obj = _asmb.CreateInstance( "TPhotoRipperPlugIn.PlugIn" ); //由該物件 query 出 plugin 共通的 interface _plugIn = (IPhotoRipperPlugIn)_obj; } catch (Exception e) { _lastError = e.Message; } } public bool isReady { get { return _plugIn != null; } } public bool hasError { get { return _lastError != ""; } } public string lastError { get { return _lastError; } } // 由此 property 就可以使用 query 出來的 IPhotoRipper interface public IPhotoRipperPlugIn plugIn { get { return _plugIn; } } } 不過這只是單一用途 plug-in,其它多種類 plug-in 串連基本上差不多 但是要考慮的東西就更多了,多種的宣告可能會像這樣 interface IURLFetcher { string Fetch(string URL); } interface IURLParser { string PlugInName {get;} void SetURLFetcherPlugIn(interface IURLFetcher); } 由多個 interface 組合起來 --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 112.104.91.27 ※ 編輯: toki 來自: 112.104.91.27 (09/25 00:27) ※ 編輯: toki 來自: 112.104.91.27 (09/25 00:28)
1F:推 Syung1983:感謝大大的範例,先研究看看 09/29 09:17







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BabyMother站內搜尋

TOP