也来学学插件式开发

上一家公司有用到插件式开发来做一个工具箱,类似于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.mzph.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…

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…

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…

视觉工程师面试指南_选择正确视觉效果的终极指南

视觉工程师面试指南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…

问题反馈模板_使用此模板可获得更好,更有价值的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;您需…

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…

保护程序猿滴眼睛-----修改VS 2008 编辑器颜色 (修改 chrome浏览器的背景色)

前几天更改了 chrome 的背景色后&#xff0c;虽然有些地方看起来不和谐&#xff0c;想百度的首页&#xff0c;显示出了大快的图片区域&#xff0c;但是&#xff0c;整体感觉这个颜色设置真的对眼睛有一定保护作用。。。 所以&#xff0c;再顺便修改一下 经常用的 vs2008 编辑器…

数据可视化 信息可视化_可视化哲学的黎明

数据可视化 信息可视化Note: this is the foreword of the book Data Visualization in Society (Amsterdam University Press, 2020)注意&#xff1a;这是《 社会中的数据可视化 》一书的前言 (阿姆斯特丹大学出版社&#xff0c;2020年) Geographer John Pickles once wrote …

HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面...

详细错误&#xff1a;HTTP 错误 404.2 - Not Found. 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置&#xff0c;无法提供您请求的页面. 出现环境&#xff1a;win7 IIS7.0 解决办法&#xff1a;IIS的根节点->右侧“ISAPI和CGI限制”->把禁止的DotNet版本项设置为允许…

重口味动漫_每种口味的图标样式

重口味动漫The icons are, without a doubt, one of the most used graphic elements today in the interface design of digital products. And to make this statement with some degree of certainty, we do not even need a very robust statistical analysis. Just rememb…

从头开始vue创建项目_我正在以设计师的身份开始一个被动的收入项目。 从头开始。...

从头开始vue创建项目Do you ever read an article on Medium (or elsewhere) about passive income, side projects and big money making blogs? When I read such an article it looks like it is easy to do yourself if you just put in the work. To see if that is the …

Exaple2_1(显示转换)

public class Example2_1{ public static void main(String arg[]){ char ca; System.out.println(""c"unicode:"(int)c); System.out.println(":"); for(int i(int)c;i<c25;i){ System.out.println(""(char)i); } }}转载于…