//Loads the possible plugin assemblies in a separate AppDomain //in order to allow unloading of unneeded assemblies public void LoadPlugins() { m_Plugins = new PluginCollection(); if (SearchDirectory == null || !Directory.Exists(SearchDirectory)) { throw new DirectoryNotFoundException("Could not find plugin directory"); } //The class actually created a new instance of the same class //only in a different AppDomain. It then passes it all the needed //parameters such as search path and file extensions. AppDomain domain = AppDomain.CreateDomain("DynamicPluginLoader"); Assembly asm = domain.Load(Assembly.GetExecutingAssembly().FullName); DynamicFindPluginProvider finder = (DynamicFindPluginProvider )asm.CreateInstance(typeof(DynamicFindPluginProvider).ToString()); //DynamicFindPluginProvider finder = (DynamicFindPluginProvider )domain.CreateInstanceFromAndUnwrap(,typeof(DynamicFindPluginProvider).ToString() ); finder.FileExtensionFilter = this.FileExtensionFilter; finder.SearchDirectory = this.SearchDirectory; ArrayList FoundPluginTypes = finder.SearchPath(this.SearchDirectory); AppDomain.Unload(domain); foreach (Type t in FoundPluginTypes) { try { if (t.IsAbstract) { continue; } //do not instantiate if this type has the "GenericPlugin" attribute object[] customAttribs = t.GetCustomAttributes(typeof(GenericPluginAttribute), false); if (customAttribs.Length > 0) { continue; } IPlugin plugin = (IPlugin )Activator.CreateInstance(t); m_Plugins.Add(plugin); } catch (Exception e) {} } }
// private void InitPlugins() // { // PluginLoader loader = new PluginLoader(); // loader.FileName="plugins.xml"; // if(loader.ReadFile() && loader.LoadPlugins()) // { // m_Plugins= loader.LoadedPlugins; // } // foreach (IPlugin plugin in m_Plugins) // { // plugin.OnInit(AppContext.Instance); // } // } private static void InitPlugins(AppContext context) { DynamicFindPluginProvider loader = new DynamicFindPluginProvider(); loader.FileExtensionFilter="*.dll"; loader.SearchDirectory= AppContext.Instance.Settings.BaseDir; loader.LoadPlugins(); foreach (IPlugin plugin in loader.LoadedPlugins) { if(plugin!=null) { context.Plugins.Add(plugin); plugin.OnInit(context); } } }