private void AddPlugin(string FileName) { //Create a new assembly from the plugin file we're adding.. Assembly pluginAssembly = Assembly.LoadFrom(FileName); //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 { Type typeInterface = null; //Gets a type object of the interface we need the plugins to match try { typeInterface = pluginType.GetInterface("SBPluginInterface.SBPlugin"); } catch { return; } //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 newPlugin = new AvailablePlugin(); //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 = (SBPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); //newPlugin.Instance.PluginHost = host; //Set the Plugin's host to this class which inherited IPluginHost //newPlugin.Instance.Host = this; //Call the initialization sub of the plugin //newPlugin.Instance.Initialize(); //Add the new plugin to our collection here this.colAvailablePlugins.Add(newPlugin); //cleanup a bit newPlugin = null; } typeInterface = null; //Mr. Clean } } } pluginAssembly = null; //more cleanup }
//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 pluginToAdd) { this.List.Add(pluginToAdd); }
/// <summary> /// Remove a Plugin to the collection of Available plugins /// </summary> /// <param name="pluginToRemove">The Plugin to Remove</param> public void Remove(AvailablePlugin pluginToRemove) { this.List.Remove(pluginToRemove); }