也來學學插件式開發

上一家公司有用到插件式開發來做一個工具箱,類似于QQ電腦管家,有很多工具列表,點一下工具下載后就可以開始使用了。可惜在那家公司待的時候有點短,沒有好好研究一下。現在有空,自己在網上找了些資料,也來試試。

?主要思路:公開一個插件接口,如果.DLL或.EXE的代碼中有繼承這個接口就將其示為插件,并將這些插件放在同一目錄。運行程序的時候掃描目錄并通過反射判斷.DLL或.EXE中是否存在該接口,若存在,則當作插件加載進來。

我們來做一個示例看看。例子也是在園子里找的,自己改了一下,詳見:http://www.cnblogs.com/xianhong/archive/2011/03/18/1988191.html

1 建立一個WinForm宿主容器。

2 定義插件接口。

?接口定義的很簡單,就兩個屬性,一個是插件要顯示的第一個界面,第二個是插件的圖標。當然,根據業務需求你可以加很多其它信息。

   public interface Iplugin{//設置加載的主窗體
        Form MainForm{get;}//插件顯示圖片
        Image ModulePicture{get;}}

?3 定義加載插件的方法。

    public static class PluginLoader{public static List<Iplugin> plugins = new List<Iplugin>();/// <summary>/// 判斷DLL中是否繼承了Iplugin接口/// </summary>/// <param name="t"></param>/// <returns></returns>private static bool IsValidPlugin(Type t){bool ret = false;Type[] interfaces = t.GetInterfaces();foreach (Type theInterface in interfaces){if (theInterface.FullName == "PluginDemo.Interface.Iplugin"){ret = true;break;}}return ret;}/// <summary>/// 加載插件,這里是在Debug的plugin目錄中搜索插件/// </summary>public static void LoadAllPlugins(){string[] files = Directory.GetFiles(Application.StartupPath + "\\plugin\\");int i = 0;foreach (string file in files){string ext = file.Substring(file.LastIndexOf("."));if (ext != ".dll") continue;try{
// 加載插件Assembly tmp
= Assembly.LoadFile(file);Type[] types = tmp.GetTypes();bool ok = false;foreach (Type t in types)if (IsValidPlugin(t)){
// 通過反射實例化Iplugin plugin
= (Iplugin)tmp.CreateInstance(t.FullName);plugins.Add(plugin);ok = true;if (ok) break;}}catch (Exception err){throw err;}}}}

4 顯示插件

?顯示插件的方法,主要是顯示插件的主界面就可以了。

 /// <summary>/// 初始化插件,顯示在列表中/// </summary>/// <param name="plugin"></param>private void InitModule(Iplugin plugin){PictureBox picture = new PictureBox();picture.BackColor = System.Drawing.Color.Red;picture.Image = plugin.ModulePicture;picture.InitialImage = null;picture.Dock = DockStyle.Left;picture.Size = new System.Drawing.Size(65, 71);picture.TabStop = false;panel1.Controls.Add(picture);//單擊時加載插件主界面picture.Click += (sender, e) =>{LoadFrm(plugin.MainForm);};}/// <summary>/// 加載插件主界面/// </summary>/// <param name="frm"></param>public void LoadFrm(Form frm){frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;frm.ShowIcon = false;frm.ShowInTaskbar = false;frm.BackColor = Color.White;//要將是否頂級控件設置為false,否則會報錯frm.TopLevel = false;frm.Dock = DockStyle.Fill;TabPage tab = new TabPage(frm.Name);tab.Controls.Add(frm);tabControl1.TabPages.Add(tab);tab.Show();frm.Show();frm.BringToFront();frm.Focus();}

?5 增加插件

?前期準備差不多了,現在我們新建一個插件來測試一下:一個簡單的顯示當前時間的界面。新建一個類庫項目,添加一個Windows窗體,然后拉一個textbox,在后臺將textboxt的文本設置為當前時間。

       private void TimeTip_Load(object sender, EventArgs e){textBox1.Text = DateTime.Now.ToString();}

生成項目,將輸出路徑改到上一個項目中的Debug中的plugin目錄中。

運行項目后效果如圖:

我們已經檢測到該插件了。點擊后就會加載該插件。

?我們要增加一個顯示日期的插件:DataPicker

加載后效果如圖:

示例代碼下載:點我

轉載于:https://www.cnblogs.com/Gyoung/archive/2013/02/20/2917070.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274355.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274355.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274355.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

同態加法_我對同態的想法

同態加法Early February, I uploaded this shot onto Dribbble. Nothing fancy –– just two screens experimenting with “2月初&#xff0c;我將這張照片上傳到Dribbble。 沒什么幻想–只有兩個屏幕在嘗試“ Neumorphism,” or soft UI. Little did I know that this post…

php內核探索

引自&#xff1a;http://www.nowamagic.net/librarys/veda/detail/1285 SAPI:Server Application Programming Interface 服務器端應用編程端口。研究過PHP架構的同學應該知道這個東東的重要性&#xff0c;它提供了一個接口&#xff0c;使得PHP可以和其他應用進行交互數據。 本…

hp-ux鎖定用戶密碼_UX設計101:用戶研究-入門需要了解的一切

hp-ux鎖定用戶密碼這是什么&#xff1f; (What is this?) This session is part of a learning curriculum that I designed to incrementally skill up and empower a team of Designers and Researchers whose skillset and ways of working needed to evolve to keep up wi…

等比數列前N項和的公式推導

設等比數列的前n項和為S(n), 等比數列的第一項為a1&#xff0c;比值為q。 &#xff08;1&#xff09;S(n) a1 a1 * q a1 * q ^ 2 .... a1 * q ^ (n - 1);&#xff08;2&#xff09;S(n1) a1 a1 * q a1 * q ^ 2 .... a1 * q ^ (n - 1) a1 * q ^ n;由&#xff08;2&am…

extjs6 引入ux_關于UX以及如何擺脫UX的6種常見誤解

extjs6 引入uxDo you ever browse social media, internet, or talk to colleagues and hear them say something UX related you disagree with so much that you just want to lecture them on the spot?您是否曾經瀏覽過社交媒體&#xff0c;互聯網或與同事交談&#xff0c…

Cocos2D-HTML5開源2D游戲引擎

http://www.programmer.com.cn/12198/ Cocos2D-HTML5是基于HTML5規范集的Cocos2D引擎的分支&#xff0c;于2012年5月發布。Cocos2D-HTML5的作者林順將在本文中介紹Cocos2D-HTML5的框架、API、跨平臺能力以及強大的性能。Cocos2D-HTML5是Cocos2D系列引擎隨著互聯網技術演進而產生…

illustrator下載_Illustrator筆工具練習

illustrator下載Adobe Illustrator is a fantastic vector creation tool and you can create a lot of things without ever using the Pen Tool. However, if you want to use Illustrator at its full potential, I personally believe that you need to master and become …

怎么更好練習數位板_如何設計更好的儀表板

怎么更好練習數位板重點 (Top highlight)Dashboard noun \?dash-?b?rd\ A screen on the front of a usually horse-drawn vehicle to intercept water, mud, or snow.儀表盤 名詞\ ?dash-?b?rd \\通常在馬拉的車輛前部的屏幕&#xff0c;用來攔截水&#xff0c;泥或雪。…

學習正則表達式

deerchao的blog Be and aware of who you are. 正則表達式30分鐘入門教程 來園子之前寫的一篇正則表達式教程&#xff0c;部分翻譯自codeproject的The 30 Minute Regex Tutorial。 由于評論里有過長的URL,所以本頁排版比較混亂,推薦你到原處查看,看完了如果有問題,再到這里來提…

人物肖像速寫_去哪兒? 優步肖像之旅

人物肖像速寫In early 2018, the Uber brand team started a rebranding exercise, exploring a fresh take on what it means to be a global transportation and technology company. A new logo was developed in tandem with a bespoke sans-serif typeface called Uber Mo…

js獲取和設置屬性

function square(num){ var total num*num;//局部變量 return total;}var total 50;//全局變量var number square(20);alert(total);//結果為50function square(num){ total num*num;//全局變量 return total;}var total 50;//全局變量var number square(20)…

hp-ux鎖定用戶密碼_我們如何簡化925移動應用程序的用戶入門— UX案例研究

hp-ux鎖定用戶密碼Prologue: While this is fundamentally a showcase of our process in the hopes of helping others, it’s also a story about the realism of limitations when working with clients and how we ultimately were able to deliver a product the client w…

微信公眾號無需二次登錄_您無需兩次解決問題-您需要一個設計系統

微信公眾號無需二次登錄重點 (Top highlight)The design system concept can be differently defined according to each person’s background. Designers may say that a design system is a style guide while developers may say it is UI standards, or specs, or even as…

android中AsyncTask和Handler對比

1 &#xff09; AsyncTask實現的原理,和適用的優缺點 AsyncTask,是android提供的輕量級的異步類,可以直接繼承AsyncTask,在類中實現異步操作,并提供接口反饋當前異步執行的程度(可以通過接口實現UI進度更新),最后反饋執行的結果給UI主線程. 使用的優點: l 簡單,快捷 l 過程可…

視覺工程師面試指南_選擇正確視覺效果的終極指南

視覺工程師面試指南When it comes to effective data visualization, the very first and also the most critical step is to select the right graph/visual for the data that you want to present. With a wide range of visualization software that is available offerin…

在 Linux 下使用 水星MW150cus (RTL8188CUS芯片)無線網卡

Fedora &#xff08;如果你不使用 PAE 內核&#xff0c;請去掉 PAE 字樣&#xff09;:yum install gcc kernel-PAE kernel-PAE-devel kernel-headers dkms Ubuntu: apt-get install make gcc linux-kernel-devel linux-headers-uname -r安裝原生驅動 注意&#xff1a;由于在 Li…

問題反饋模板_使用此模板可獲得更好,更有價值的UX反饋

問題反饋模板Feedback is an important part of UX design. To improve the work you do you need to be able to give and receive feedback. Receiving valuable feedback is for a very large part up to you.反饋是UX設計的重要組成部分。 為了改進您的工作&#xff0c;您需…

【轉載】Android Animation 簡介(官方文檔翻譯) ---- 翻譯的很好!

http://vaero.blog.51cto.com/4350852/849783轉載于:https://www.cnblogs.com/DonkeyTomy/articles/2945687.html

ubuntu 如何轉換 ppk ,連接 amazon ec2

轉 自 &#xff1a;http://www.ehow.com/how_8658327_convert-ppk-ssh-ubuntu.html1 Open a terminal window in Ubuntu, or log in if you are converting the keys on a remote Ubuntu server. 2 Type "sudo apt-get install putty-tools" at the terminal prompt …

iofd:文件描述符_文字很重要:談論設計時18個有意義的描述符

iofd:文件描述符As designers, many of us think we’re just visual creatures. But creating visuals is only half of the job. The other half is verbal communication — actually talking about design. Whether we’re showcasing our own work, giving or receiving c…