zoukankan      html  css  js  c++  java
  • [转载]插件开发

     

    winform程序,很多时候都需要用到插件式的,所以本人做了一个Demo,思路跟网上思路基本一致,现在共享出来如有需要的朋友可以下载。

    申明:部分代码来源于网上,当然思路也是,呵呵

    原理很简单:

    一:定义插件接口

    二:实现插件接口并建立不同工项目,使其在生成时生成不同的DLL

    三:主程序运行时根据接口名利用反射对插件目录的DLL进行加载,加载完成后便可以使用插件接口定义的方法或属性了。

    下面上几张图,有兴趣的朋友可以先看看,觉得值得一看的朋友可以下载。

    项目结构:

    DefaultPlugin,PosPlugin两个项目均为插件,均实现了Iplugin接可

    Iplugin接口
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using System.Windows.Forms;

    namespace WinDemo.Core
    {
        
    public interface Iplugin
        {

            PluginInfoAttribute PluginInfo
            { 
    getset; }
            
    bool IsLoad
            {
                
    get;
                
    set;
            }

            Image ModulePicture
            {
                
    get;
            }

            Image ModulePictureEnter
            {
                
    get;
            }

            Image ModulePictureClick
            {
                
    get;
            }

            
    string ModuleName
            {
                
    get;
            }


            Dictionary
    <string, EventHandler> ChildNodes
            {
                
    get;
            }

            ILoadForm FormLoader
            {
                
    get;
                
    set;
            }

           
        }
    }

     

    用于加载插件的类 PluginLoader
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.IO;
    using System.Windows.Forms;
    using System.Collections;

    namespace WinDemo.Core
    {
        
    public static class PluginLoader
        {

            
    public static List<Iplugin> plugins = new List<Iplugin>();
            
    private static ArrayList piProperties = new ArrayList();
            
            
    private static bool IsValidPlugin(Type t)
            {
                
    bool ret = false;
                Type[] interfaces 
    = t.GetInterfaces();
                
    foreach( Type theInterface in interfaces ) {
                    
    if (theInterface.FullName == "WinDemo.Core.Iplugin")
                    {
                        ret 
    = true;
                        
    break;
                    }
                }
                
    return ret;
            }


           

            
    public static void LoadAllPlugins()
            {
                
    string[] files = Directory.GetFiles(Application.StartupPath + "\\plugin\\");
                
    int i = 0;
                PluginInfoAttribute typeAttribute 
    = new PluginInfoAttribute();
                
    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);
                                
    object[] attbs = t.GetCustomAttributes(typeAttribute.GetType(), false);
                                PluginInfoAttribute attribute 
    = null;
                                
    foreach (object attb in attbs)
                                {
                                    
    if (attb is PluginInfoAttribute)
                                    {
                                        attribute 
    = (PluginInfoAttribute)attb;
                                        attribute.Index 
    = i;
                                        i
    ++;
                                        ok 
    = true;
                                        
    break;
                                    }
                                }

                                
    if (attribute != null)
                                {
                                    piProperties.Add(attribute);
                                    plugin.PluginInfo 
    = attribute;
                                }
                                
    else throw new Exception("未定义插件属性");
                                
    if (ok) break;
                            }
                    }
                    
    catch (Exception err)
                    {
                        
    throw err;
                    }
                }
                plugins.Sort((p1, p2) 
    => {
                    
    return p2.PluginInfo.Index - p1.PluginInfo.Index;
                });
            }
        }
    }

      

    Iplugin的实现类PosPlugin类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using WinDemo.Core;
    using System.Windows.Forms;
     

    namespace DefaultPlugin
    {
        [PluginInfo(
    "Default","1.0","XH","www.cnporter.com",true,2)]
        
    public class Default : WinDemo.Core.Iplugin
        {
            
    private Dictionary<string, EventHandler> _ChildNodes = new Dictionary<string, EventHandler>();
            
    private LeftNav frmLeftNav = new LeftNav(); 
            
    public static ILoadForm Formloader ;
            
    public Default()
            {

                _ChildNodes.Add(
    "菜单五", (sender,e) =>
                {
                    MessageBox.Show(sender.ToString());
                });
                _ChildNodes.Add(
    "菜单四", (sender, e) =>
                {
                    MessageBox.Show(sender.ToString());
                });
                _ChildNodes.Add(
    "菜单三", (sender, e) =>
                {
                    MessageBox.Show(sender.ToString());
                });
                _ChildNodes.Add(
    "菜单二", (sender, e) =>
                {
                    MessageBox.Show(sender.ToString());
                });
                _ChildNodes.Add(
    "菜单一", (sender, e) =>
                {
                    FormLoader.LoadNavFrm(frmLeftNav);
                });
            }

            

            
    public Image ModulePicture
            {
                
    get
                {
                    
    return ((System.Drawing.Image)(ImageResource.Index));
                }
            }

            
    public Image ModulePictureEnter
            {
                
    get
                {
                    
    return ((System.Drawing.Image)(ImageResource.IndexEnter));
                }
            }

            
    public Image ModulePictureClick
            {
                
    get
                {
                    
    return ((System.Drawing.Image)(ImageResource.IndexClick));
                }
            }

            
    public string ModuleName
            {
                
    get
                {
                    
    return "首页";
                }
            }

            
    public Dictionary<string, EventHandler> ChildNodes
            {
                
    get
                {
                    
    return _ChildNodes;
                }
            }




            
    public bool IsLoad
            {
                
    get;
                
    set;
            }
            
            
    public ILoadForm FormLoader
            {
                
    get
                {
                    
    return Formloader;
                }
                
    set
                {
                    Formloader 
    = value;
                }
            }



            
    public PluginInfoAttribute PluginInfo
            {
                
    get;
                
    set;
            }
        }

     
    }
     

    此图现在有两个插件

     

    运行效果如下

     

     

     

     

    -------------------------------------------------------------------

    一般的程序,需要修改功能、扩展功能时,需要修改程序的代码,当功能变动很大时,代码的修改非常繁琐。

           插件式开发,就是把程序功能封装在不同的插件中,主程序调用不同的插件可以实现各种功能,增加了程序的扩展性和变更性。

           插件开发的流程一般如下:

    1.定义程序的功能   (通过一些接口定义各个功能

    2.开发功能插件     (功能实现,存放在dll文件中)
    3.主程序调用插件(dll文件),实现各种功能

    插件示例:

    1、编写插件

    代码
    //1 定义插件接口,将其编译成 dll,例如:
    using System;

    namespace PluginInterface
    {
        
    public interface IShow
        {
            
    string Show();
        }
    }

    //2 编写插件A. 新建dll工程,并引用第一步做的dll插件,实现其接口,例如:
    namespace PluginA
    {
        
    public class PluginA : PluginInterface.IShow
        {
            
    public string Show()
            {
                
    return "I am plugin A";
            }
        }
    }

    //3 编写插件B. 新建dll工程,并引用第一步做的dll插件,实现其接口,插件B中多一个函数ShowMoney(),例如:
    namespace PluginB
    {
        
    public class PluginB : PluginInterface.IShow
        {
            
    public string Show()
            {
                
    return "I am plugin B";
            }
            public string ShowMoney(string Name)
            {
                
    return Name+" have 10000000 dollar!!!";
            }

        }
    }

    2、在主程序中收集或载入插件

    代码
    //3. 在指定目录下寻找Dll文件
    private void frmMain_Load(object sender, System.EventArgs e)
    {
        
    //获取Plugins目录中所有的DLL文件,并保存在cmbPlugins(comobox)中
        try
        {
            
    string path = Application.StartupPath;         //程序所在目录
            path = System.IO.Path.Combine(path, "Plugins");    //程序目录下的Plugins文件夹
            
    foreach (string file in System.IO.Directory.GetFiles(path, "*.dll"))
            {
                
    this.cmbPlugins.Items.Add(file);     //将Plugins文件夹中的所有dll文件路径加入到cmbPlugins中
            }
        }
        
    catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    3、使用插件

      3.1 利用反射和插件的接口来实现插件功能

    代码
    private void btnExecute_Click(object sender, System.EventArgs e)
    {
        
    try
        {
            
    //1. 获得 文件名称 
            
    string asmFile = this.cmbPlugins.Text;
            
    string asmName = System.IO.Path.GetFileNameWithoutExtension(asmFile);

            
    if (asmFile != string.Empty)
            {
                
    //2. 利用反射,构造DLL文件的实例
                System.Reflection.Assembly asm 
    = System.Reflection.Assembly.LoadFrom(asmFile);

                
    //3. 利用反射,从程序集(DLL)中,提取类,并把此类实例化,利用接口来实现类的功能
                 Type type = asm.GetType(asmName + "." + asmName);//必须使用名称空间+类名称
                 PluginInterface.IShow iShow 
    = (PluginInterface.IShow)System.Activator.CreateInstance(type);

                
    //4. 在主程序中使用接口功能
                 MessageBox.Show(iShow.Show());
            }
        }
        
    catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

          当选择PluginA的dll时,程序会弹出"I am plugin A";
          当选择PluginB的dll时,程序会弹出"I am plugin B";

       3.2 直接利用反射来实现插件功能,例如使用PluginB中的ShowMoney()功能

    代码
    private void btnExecute_Click(object sender, System.EventArgs e)
            {
                try
                {
                    //1. 获得 文件名称 
                    
    string asmFile = this.cmbPlugins.Text;
                    
    string asmName = System.IO.Path.GetFileNameWithoutExtension(asmFile);
                   
                    if (asmFile != string.Empty)   //当插件不为空
                    {
                        if (asmName == PluginB)    //当选择的是插件B时
                        {
                         //2. 利用反射,构造DLL文件的实例
                         System.Reflection.Assembly asm 
    = System.Reflection.Assembly.LoadFrom(asmFile);
                         Type type 
    = asm.GetType(asmName + "." + asmName);//必须使用名称空间+类名称
                        
                          //3. 利用反射,从程序集(DLL)中,提取类,并把此类实例化
                          object obj = System.Activator.CreateInstance(type);
                        
                          //4. 定义PluginB中类函数ShowMoney()的参数Name
                          object[] parameters = new string[]{"Zhangfei"}); 
                         
                          //5.调用PluginB中的函数ShowMoney(),参数Name设置为为parameters
                          System.Reflection.MethodInfo method = type.GetMethod("ShowMoney");//方法的名称
                          MessageBox.Show((string)method.Invoke(obj, parameters));
                         }
                     }
                }
                
    catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }

    果选择的是PluginB的dll文件,程序会弹出 “Zhangfei have 10000000 dollar!!!”。 

  • 相关阅读:
    GridView自定义分页
    intro
    ListView和DataPager初试
    在DataGrid中,如何录入数量及单价的时候自动算出金额
    常用正则表达式
    ASP.NET中基类页的设计和使用
    Asp.net实现无刷新检测用户名
    在asp.net2.0中使用存储过程
    .NET中的抽象工厂
    用C#生成随机中文汉字验证码的基本原理 [转]
  • 原文地址:https://www.cnblogs.com/fx2008/p/2262000.html
Copyright © 2011-2022 走看看