using System; using System.IO; using System.Runtime; using System.Reflection; using System.Collections.Generic; namespace Ion.Scripting { public class PluginManager { #region Fields private List mPlugins = new List(); #endregion #region Methods /// /// Unloads the current plugins and clears the plugin collection. /// public void Clear() { lock (mPlugins) { foreach (IonPlugin pPlugin in mPlugins) { pPlugin.Unload(); } mPlugins.Clear(); } } public List GetPlugins(string sDirectory) { List pPlugins = new List(); if (Directory.Exists(sDirectory)) { string[] szFiles = Directory.GetFiles(sDirectory, "*.dll"); for (int x = 0; x < szFiles.Length; x++) { IonPlugin pPlugin = this.InstantiatePluginFromAssembly(szFiles[x]); if (pPlugin != null) pPlugins.Add(pPlugin); } } return pPlugins; } /// /// Tries to instantiate and return an IonPlugin descendant by loading a .NET assembly file at a given path. Null is returned if the target file does not exist, is not a .NET assembly file or is not a IonPlugin descendant. /// /// The full path to the assembly file to load. private IonPlugin InstantiatePluginFromAssembly(string sAssemblyPath) { try { Assembly pAssembly = Assembly.Load(AssemblyName.GetAssemblyName(sAssemblyPath)); IonPlugin pPlugin = (IonPlugin)Activator.CreateInstance(pAssembly.GetType()); return pPlugin; } catch { return null; } // Either file not found, no CLI header or no IonPlugin deriver } #endregion } }