·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 基于.net 职责链来实现 插件模式

基于.net 职责链来实现 插件模式

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23
基于.net 职责链来实现 插件模式
    •   插件式的例子
  1. QQ电脑管家,有很多工具列表,点一下工具下载后就可以开始使用了
  2. eclipse,X Server等等
    •   插件式的好处
  1. 插件降低框架的复杂性,把扩展功能从框架中剥离出来
  2. 让第三方有机会来扩展程序的功能
    •   思路

公开一个插件接口,如果.DLL或.EXE的代码中有继承这个接口就将其示为插件,并将这些插件放在同一目录。运行程序的时候扫描目 录并通过反射判断.DLL或.EXE中是否存在该接口,若存在,则当作插件加载进来。如下图示

  • 基于.net职责链来实现插件模式

1.定义命令接口

     public interface ICommand    {        ServerReturn execute();        ServerReturn Rollback();    }

获取当前目录下继承该接口的方法

        public  List<ICommand> CommandList()        {            List<ICommand> ICommandList = new List<ICommand>();            string[] files = Directory.GetFiles(System.IO.Directory.GetCurrentDirectory());            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 (IsValidCommand(t))                        {                            // 通过反射实例化                            ICommand plugin = (ICommand)tmp.CreateInstance(t.FullName);                            ICommandList.Add(plugin);                            ok = true;                            if (ok) break;                        }                }                catch (Exception err)                {                    throw err;                }            }            return ICommandList;        }        /// <summary>        /// 判断DLL中是否继承了ICommand接口        /// </summary>        /// <param name="t"></param>        /// <returns></returns>        PRivate static bool IsValidCommand(Type t)        {            bool ret = false;            Type[] interfaces = t.GetInterfaces();            foreach (Type theInterface in interfaces)            {                if (theInterface.FullName == "ClassDemo.ICommand")                {                    ret = true;                    break;                }            }            return ret;        }

职责链执行方法组

        /// <summary>        /// 方法执行        /// </summary>        public void exec()        {            List<ICommand> list = new List<ICommand>();            foreach( ICommand demo in list)            {                if(!demo.execute().isSurccess)                {                    demo.Rollback();                    return;                }            }        }