/// <summary> /// 在指定的路径中查找服务提供者 /// </summary> /// <param name="loaderPath">文件夹列表</param> /// <returns>查找的结果</returns> public static PluginList GetPlugins(params string[] loaderPath) { PluginList list = new PluginList(); Action<string> loader = s => { PluginInfo[] slist = GetPluginsInAssembly(s); if (slist != null) list.AddRange(slist); }; Action<string> folderLoader = s => { if (!System.IO.Path.IsPathRooted(s)) s = System.IO.Path.Combine( System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), s); string[] files = System.IO.Directory.GetFiles(s, "*.exe"); Array.ForEach(files, loader); files = System.IO.Directory.GetFiles(s, "*.dll"); Array.ForEach(files, loader); }; folderLoader(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)); Array.ForEach(loaderPath, folderLoader); return list; }
public void DistributeEvent(String eventName, MyEventArgBase e) { if (Broadcast(eventName, e)) { return; } // Not a broadcast; process event PluginList pluginList = new PluginList(); lock (s_subscriberLock) { PluginList tmpList; if (m_Subscribers.TryGetValue(eventName, out tmpList)) { // Deep copy pluginList.AddRange(tmpList); } } foreach (IMyPlugin plugin in pluginList) { if ((plugin.EventPriority <= e.CeilingPriority) || (plugin.EventPriority >= e.FloorPriority)) { // the source should not be involved. if (!plugin.Equals(e.MyPlugin)) { plugin.HandleEvent(eventName, e); } } } }
public PluginList GetPlugins() { var iPluginName = typeof(IPlugin).FullName; var list = new PluginList(); string[] files; try { files = Directory.GetFiles(PluginDirectoryPath, "*.dll") .Concat(Directory.GetDirectories(PluginDirectoryPath) .SelectMany(x => Directory.GetFiles(x, "*.dll"))) .ToArray(); } catch { return new PluginList(); } foreach (var path in files) { try { var assembly = Assembly.LoadFile(path); list.AddRange(assembly.GetTypes() .Where(x => x.IsClass && !x.IsAbstract && !x.IsNotPublic && x.GetInterface(iPluginName) != null) .Select(x => new ExternalPlugin( GetRelativeFileNameWithoutExtension(path), (IPlugin)Activator.CreateInstance(x)))); } catch { continue; } } return list; }