也来学学插件式开发

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

等比数列前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…