插件程序开发

来源:互联网 发布:网络用语mjd什么意思 编辑:程序博客网 时间:2024/05/17 09:35

一、什么是插件程序?

      插件是实现了某种软件接口程序模块,第三方开发者可以遵循该接口开发独立的功能。插件模块通常以独立的文件形式存在,例如OCX文件等,也可能是一组文件的形式存在。

提起插件式,我们首先想到的是firefox,用过firefox的人都知道它是一个插件式程序。当一个功能需要,完全可以从网上下载一个插件后,重启后,就能使用。这个功能给我们带来许多的方便之处,这就是插件式程序的好处。

      插件的本质在于不修改程序主体(平台)的情况下对软件功能进行拓展与加强,当插件的接口公开后,任何公司或个人都可以制作自己的插件来解决一些操作上的不便或增加新功能,也就是真正意义上实现“即插即用”软件开发。

      平台+插件软件结构是将一个待开发的目标软件分为两部分,一部分为软件的主体或框架,可定义为平台,这是预先编译后的程序。另一部分为功能或补充模块,可定义为插件。这个就是后来要进行安装的插件程序。

      假设你的程序已经部署在用户的计算机上,并且能够正常运行了。但是有一天,用户打来电话——他们需要增加新的功能。确定了用户的需求后,你竟然发现原有的软件架构已经无法胜任新增任务的需求——你需要重新设计这个应用了!但问题是,就算你又用了一个开发周期完成了用户需要的应用,切不能保证用户的需求不会再次变更。也就是说,需求蔓延的可能性依然存在。因此,这种情况下插件架构更能显示出它的优越性。

       可以这么说,用它可以带来方便的地方,而且开发它,也很简单。而且这样的主程序根本就不需要改动。需要插件时,拿来就能用,插件更新时,也只需更新这个插件即可。

       从程序开发这角度,一般是先开发主程序,决定哪些功能由主程序来完成,然后再建立接口,申明接口的内容,这些内容决定着插件功能的扩展,及方向的。这些都是有主程序开发者预先准备好的。插件开发者,从主程序开发者那里得到接口的内容,并书写继承这些接口的类,来完成具体的功能。

二、插件程序举例。

1、新建一个类库,并生成.dll(选择项目,点击“生成”,在该项目文件资源管理器的Debug中即可发现)。

复制代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace IMsg{    /// <summary>    /// 这是插件必须实现的接口,也是主程序与插件通信的唯一接口    /// 换句话说,主程序只认识插件里的这些方法    /// </summary>    public interface IMsgPlug    {        void OnShowDlg();        string OnShowInfo();    }}
复制代码

 

2、再次新建一个类库,分别创建类MyConsole和MYDlg,同时添加对IMsg的引用,生成.dll。

复制代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using IMsg;namespace MYPlugin1{    public class MyConsole : IMsgPlug    {        #region IMsgPlug 成员        public void OnShowDlg()        {            Console.WriteLine("控制台调用插件的OnShowDlg方法");        }        public string OnShowInfo()        {            return "myConsole";        }        #endregion    }}
复制代码
复制代码
using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using IMsg;namespace MYPlugin1{    public class MYDlg : Form, IMsgPlug    {        #region IMsgPlug 成员        public void OnShowDlg()        {            this.Text = "插件子窗体";            this.ShowDialog();//调用Form的ShowDialog,显示窗体        }        public string OnShowInfo()        {            return "MyDlg";        }        #endregion    }}
复制代码

 

  3、在解决方案资源管理器中添加窗体项目WindowsFormsApplication,修改Form1.cs为如下:

复制代码
using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {/// <summary>        /// 存放插件的集合        /// </summary>        private ArrayList plugins = new ArrayList();                public Form1()        {            InitializeComponent();        }        /// <summary>        /// 载入所有插件        /// </summary>        private void LoadAllPlugs()        {            //获取插件目录(plugins)下所有文件            string[] files = Directory.GetFiles(Application.StartupPath + @"\Plugins");            foreach (string file in files)            {                if (file.ToUpper().EndsWith(".DLL"))                {                    try                    {                        //载入dll                        Assembly ab = Assembly.LoadFrom(file);                        Type[] types = ab.GetTypes();                        foreach (Type t in types)                        {                            //如果某些类实现了预定义的IMsg.IMsgPlug接口,则认为该类适配与主程序(是主程序的插件)                            if (t.GetInterface("IMsgPlug") != null)                            {                                plugins.Add(ab.CreateInstance(t.FullName));                                listBox1.Items.Add(t.FullName);                            }                        }                    }                    catch (Exception ex)                    {                        MessageBox.Show(ex.Message);                    }                }            }        }        private void btnLoadPlug_Click_1(object sender, EventArgs e)        {            LoadAllPlugs();            MessageBox.Show("haha");        }        private void btnExecute_Click_1(object sender, EventArgs e)        {            if (this.listBox1.SelectedIndex == -1) return;            object selObj = this.plugins[this.listBox1.SelectedIndex];            Type t = selObj.GetType();            MethodInfo OnShowDlg = t.GetMethod("OnShowDlg");            MethodInfo OnShowInfo = t.GetMethod("OnShowInfo");            OnShowDlg.Invoke(selObj, null);//使用selObj调用OnShowDlg函数            object returnValue = OnShowInfo.Invoke(selObj, null);            this.lblMsg.Text = returnValue.ToString();        }    }}
复制代码

4、将WindowsFormsApplication1设为启动项目,运行调试。

 

转自:http://www.cnblogs.com/iammackong/p/3388241.html

0 0
原创粉丝点击