/// <summary> /// Finds a plugin in the available Plugins /// </summary> /// <param name="pluginNameOrPath">The name or File path of the plugin to find</param> /// <returns>Available Plugin, or null if the plugin is not found</returns> public AvailablePlugIn <T> Find(string pluginNameOrPath) { AvailablePlugIn <T> r = null; //Loop through all the plugins foreach (AvailablePlugIn <T> pluginOn in this.List) { //Find the one with the matching name or filename if (pluginOn.AssemblyPath.Equals(pluginNameOrPath) || pluginOn.AssemblyPath.EndsWith(pluginNameOrPath)) { r = pluginOn; break; } } return(r); }
private void AddPlugin(string FileName) { //Create a new assembly from the plugin file we're adding.. Assembly pluginAssembly = Assembly.LoadFrom(FileName); string tn = typeof(T).FullName; //Next we'll loop through all the Types found in the assembly foreach (Type pluginType in pluginAssembly.GetTypes()) { if (pluginType.IsPublic) //Only look at public types { if (!pluginType.IsAbstract) //Only look at non-abstract types { //Gets a type object of the interface we need the plugins to match Type typeInterface = pluginType.GetInterface(tn, true); //Make sure the interface we want to use actually exists if (typeInterface != null) { //Create a new available plugin since the type implements the IPlugin interface AvailablePlugIn <T> newPlugin = new AvailablePlugIn <T>(); //Set the filename where we found it newPlugin.AssemblyPath = FileName; //Create a new instance and store the instance in the collection for later use //We could change this later on to not load an instance.. we have 2 options //1- Make one instance, and use it whenever we need it.. it's always there //2- Don't make an instance, and instead make an instance whenever we use it, then close it //For now we'll just make an instance of all the plugins newPlugin.Instance = (T)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); //Add the new plugin to our collection here this.mAvailablePlugins.Add(newPlugin); //cleanup a bit newPlugin = null; } typeInterface = null; //Mr. Clean } } } pluginAssembly = null; //more cleanup }
/// <summary> /// Remove a Plugin to the collection of Available plugins /// </summary> /// <param name="pluginToRemove">The Plugin to Remove</param> public void Remove(AvailablePlugIn <T> p) { this.List.Remove(p); }
//A Simple Home-brew class to hold some info about our Available Plugins /// <summary> /// Add a Plugin to the collection of Available plugins /// </summary> /// <param name="pluginToAdd">The Plugin to Add</param> public void Add(AvailablePlugIn <T> p) { this.List.Add(p); }