/// <summary> /// Resets the PluginFactory. Should olny be used in test classes. /// </summary> public static void Reset() { Info.Clear(); ParserFactories.Clear(); Analyzers.Clear(); PanelFactories.Clear(); }
/// <summary> /// Add plugins from path. Can be called multiple times to include several paths. /// </summary> /// <param name="pluginPath">The path to include plugins from.</param> public static List <Exception> LoadPlugins(string pluginPath) { if (!Directory.Exists(pluginPath)) { throw new DirectoryNotFoundException("The plugin directory (" + Path.GetFullPath(pluginPath) + ") could not be found!"); } // Save any exception and continue loading next dll List <Exception> exList = new List <Exception>(); foreach (var dll in Directory.EnumerateFiles(pluginPath, "*.dll")) { var domain = AppDomain.CurrentDomain; domain.ReflectionOnlyAssemblyResolve += MyInterceptMethod; string name = Path.GetFileName(dll); try { Assembly nextAssembly = Assembly.Load(File.ReadAllBytes(dll)); var info = new PluginInfo(string.Format("{0} v{1}", nextAssembly.GetName().Name, nextAssembly.GetName().Version), dll); foreach (var type in nextAssembly.GetTypes()) { foreach (var item in type.GetInterfaces()) { if (item.Name == nameof(IParserFactory) && !type.IsGenericType && !type.IsInterface) { IParserFactory factory = PluginFactory.CreateParserFactory(type); ParserFactories.Add(factory); ParserPluginAttribute attr = type.GetCustomAttribute <ParserPluginAttribute>(false); if (attr != null) { info.AddItem(new PluginInfo.ItemInfo() { Name = string.Format("{0} ({1})", attr.Title, attr.FileType), Type = PluginInfo.ItemType.Parser }); } } else if (item.Name == nameof(IAnalyzer) && !type.IsGenericType && !type.IsInterface) { AnalyzerPluginAttribute attr = type.GetCustomAttribute <AnalyzerPluginAttribute>(false); Analyzers.Add(type); info.AddItem(new PluginInfo.ItemInfo() { Name = type.Name, Type = PluginInfo.ItemType.Analyzer }); } else if (item.Name == nameof(IPanelFactory) && !type.IsGenericType && !type.IsInterface) { IPanelFactory factory = PluginFactory.CreatePanelFactory(type); PanelFactories.Add(factory); info.AddItem(new PluginInfo.ItemInfo() { Name = factory.Title, Type = PluginInfo.ItemType.Display }); } } } // Add assembly item is containing any plugin if (info.Items.Count > 0) { Info.Add(info); } } catch (ReflectionTypeLoadException ex) { Exception exL = ex.LoaderExceptions[0]; if (exL is TypeLoadException) { exList.Add(new PluginLoadException("Plugin " + name + " could not load from " + pluginPath + "! Please contact plugin author.", exL)); } else { exList.Add(new PluginLoadException("Plugin " + name + " could not load! Please contact plugin author.", ex.LoaderExceptions[0])); } } catch (Exception ex) { exList.Add(new PluginLoadException("Plugin " + name + " could not load! Please contact plugin author.", ex)); } finally { domain.ReflectionOnlyAssemblyResolve -= MyInterceptMethod; } } // End of file loop return(exList); }