using System; using System.Collections.Generic; using System.Reflection; using System.Text; using System.IO; using Mesnac.PlugIn.View; namespace Mesnac.Gui.PlugIn { public class PlugInService { /// /// 扫描插件 /// /// 插件目录 /// 返回插件列表 public static List ScanPlugIn(string plugInPath) { List plugIns = new List(); //保存插件列表 DirectoryInfo dir = new DirectoryInfo(plugInPath); //定义插件目录对象 FileInfo[] plugInFiles = dir.GetFiles("*.dll", SearchOption.TopDirectoryOnly); //扫描插件目录下的所有插件文件,只扫描顶层目录下的dll文件 PlugInDescriptor plugIn = null; //定义插件变量 Assembly assembly = null; foreach (FileInfo fi in plugInFiles) { plugIn = new PlugInDescriptor(); #region 解析插功能名称 assembly = Assembly.LoadFile(fi.FullName); string functionName = String.Empty; //dll对应功能名称 IList lst = assembly.GetCustomAttributesData(); foreach (CustomAttributeData data in lst) { if (data.Constructor.DeclaringType.FullName == "System.Reflection.AssemblyDescriptionAttribute") { functionName = data.ConstructorArguments[0].Value as string; break; } } if (String.IsNullOrEmpty(functionName)) { foreach (CustomAttributeData data in lst) { if (data.Constructor.DeclaringType.FullName == "System.Reflection.AssemblyTitleAttribute") { functionName = data.ConstructorArguments[0].Value as string; break; } } } plugIn.FunctionName = functionName; //获取插件功能名称 #endregion #region 解析视图窗口 List viewContents = new List(); Type[] types = assembly.GetTypes(); foreach (Type t in types) { Type[] result = t.FindInterfaces(IViewContentTypeFilter, null); if (result != null && result.Length > 0) { IViewContent content = Activator.CreateInstance(t) as IViewContent; viewContents.Add(content); } } plugIn.ViewContents = viewContents; //加载窗口集合 #endregion plugIns.Add(plugIn); //把插件添加到插件列表中 } return plugIns; } /// /// 视图窗口接口过滤 /// /// /// /// public static bool IViewContentTypeFilter(Type type, object target) { if (type.FullName == "Mesnac.PlugIn.View.IViewContent") { return true; } return false; } } }