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