Commercial & open source. Find your way forward.Compare editions
OCS NETWORKS INC / OSS

IntelliDispatch OSS MIT License

The open-source edition. An MIT-licensed core, with optional paid, licensed plugins.

Code About this workspace LicenseRead-only source browser
Public files
PluginServices.cs
7.4 KB · 238 linesModified Nov 6, 2026 UTC
1using System;2using System.IO;3using System.Reflection;4using PluginInterface;56namespace IntelliDispatch 7{8	/// <summary>9	/// Summary description for PluginServices.10	/// </summary>11	public class PluginServices : IPluginHost   //<--- Notice how it inherits IPluginHost interface!12	{13		/// <summary>14		/// Constructor of the Class15		/// </summary>16		public PluginServices()17		{18		}19	20		private Types.AvailablePlugins colAvailablePlugins = new Types.AvailablePlugins();21		22		/// <summary>23		/// A Collection of all Plugins Found and Loaded by the FindPlugins() Method24		/// </summary>25		public Types.AvailablePlugins AvailablePlugins26		{27			get {return colAvailablePlugins;}28			set {colAvailablePlugins = value;}29		}30		31		/// <summary>32		/// Searches the Application's Startup Directory for Plugins33		/// </summary>34		public void FindPlugins()35		{36			FindPlugins(AppDomain.CurrentDomain.BaseDirectory);37		}38		/// <summary>39		/// Searches the passed Path for Plugins40		/// </summary>41		/// <param name="Path">Directory to search for Plugins in</param>42		public void FindPlugins(string Path)43		{44			//First empty the collection, we're reloading them all45			colAvailablePlugins.Clear();46		47			//Go through all the files in the plugin directory48			foreach (string fileOn in Directory.GetFiles(Path))49			{50				FileInfo file = new FileInfo(fileOn);51				52				//Preliminary check, must be .dll53				if (file.Extension.Equals(".dll"))54				{	55					//Add the 'plugin'56					this.AddPlugin(fileOn);				57				}58			}59		}60		61		/// <summary>62		/// Unloads and Closes all AvailablePlugins63		/// </summary>64		public void ClosePlugins()65		{66			foreach (Types.AvailablePlugin pluginOn in colAvailablePlugins)67			{68				//Close all plugin instances69				//We call the plugins Dispose sub first incase it has to do 70				//Its own cleanup stuff71				pluginOn.Instance.Dispose(); 72				73				//After we give the plugin a chance to tidy up, get rid of it74				pluginOn.Instance = null;75			}76			77			//Finally, clear our collection of available plugins78			colAvailablePlugins.Clear();79		}80		81		private void AddPlugin(string FileName)82		{83			//Create a new assembly from the plugin file we're adding..84			Assembly pluginAssembly = Assembly.LoadFrom(FileName);85			86			//Next we'll loop through all the Types found in the assembly87			foreach (Type pluginType in pluginAssembly.GetTypes())88			{89				if (pluginType.IsPublic) //Only look at public types90				{91					if (!pluginType.IsAbstract)  //Only look at non-abstract types92					{93						//Gets a type object of the interface we need the plugins to match94						Type typeInterface = pluginType.GetInterface("PluginInterface.IPlugin", true);95						96						//Make sure the interface we want to use actually exists97						if (typeInterface != null)98						{99							//Create a new available plugin since the type implements the IPlugin interface100							Types.AvailablePlugin newPlugin = new Types.AvailablePlugin();101							102							//Set the filename where we found it103							newPlugin.AssemblyPath = FileName;104							105							//Create a new instance and store the instance in the collection for later use106							//We could change this later on to not load an instance.. we have 2 options107							//1- Make one instance, and use it whenever we need it.. it's always there108							//2- Don't make an instance, and instead make an instance whenever we use it, then close it109							//For now we'll just make an instance of all the plugins110							newPlugin.Instance = (IPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));111							112							//Set the Plugin's host to this class which inherited IPluginHost113							newPlugin.Instance.Host = this;114115							//Call the initialization sub of the plugin116							newPlugin.Instance.Initialize();117							118							//Add the new plugin to our collection here119							this.colAvailablePlugins.Add(newPlugin);120							121							//cleanup a bit122							newPlugin = null;123						}	124						125						typeInterface = null; //Mr. Clean			126					}				127				}			128			}129			130			pluginAssembly = null; //more cleanup131		}132133		/// <summary>134		/// Displays a feedback dialog from the plugin135		/// </summary>136		/// <param name="Feedback">String message for feedback</param>137		/// <param name="Plugin">The plugin that called the feedback</param>138		public void Feedback(string Feedback, IPlugin Plugin)139		{140			//This sub makes a new feedback form and fills it out141			//With the appropriate information142			//This method can be called from the actual plugin with its Host Property143144			System.Windows.Forms.Form newForm = null;145			frmFeedback newFeedbackForm = new frmFeedback();146			147			//Here we set the frmFeedback's properties that i made custom148			newFeedbackForm.PluginAuthor = "By: " + Plugin.Author;149			newFeedbackForm.PluginDesc = Plugin.Description;150			newFeedbackForm.PluginName = Plugin.Name;151			newFeedbackForm.PluginVersion = Plugin.Version;152			newFeedbackForm.Feedback = Feedback;153			154			//We also made a Form object to hold the frmFeedback instance155			//If we were to declare if not as  frmFeedback object at first,156			//We wouldn't have access to the properties we need on it157			newForm = newFeedbackForm;158			newForm.ShowDialog();159160			newFeedbackForm = null;161			newForm = null;162			163		}164	}165	namespace Types166	{167		/// <summary>168		/// Collection for AvailablePlugin Type169		/// </summary>170		public class AvailablePlugins : System.Collections.CollectionBase171		{172			//A Simple Home-brew class to hold some info about our Available Plugins173			174			/// <summary>175			/// Add a Plugin to the collection of Available plugins176			/// </summary>177			/// <param name="pluginToAdd">The Plugin to Add</param>178			public void Add(Types.AvailablePlugin pluginToAdd)179			{180				this.List.Add(pluginToAdd); 181			}182		183			/// <summary>184			/// Remove a Plugin to the collection of Available plugins185			/// </summary>186			/// <param name="pluginToRemove">The Plugin to Remove</param>187			public void Remove(Types.AvailablePlugin pluginToRemove)188			{189				this.List.Remove(pluginToRemove);190			}191		192			/// <summary>193			/// Finds a plugin in the available Plugins194			/// </summary>195			/// <param name="pluginNameOrPath">The name or File path of the plugin to find</param>196			/// <returns>Available Plugin, or null if the plugin is not found</returns>197			public Types.AvailablePlugin Find(string pluginNameOrPath)198			{199				Types.AvailablePlugin toReturn = null;200			201				//Loop through all the plugins202				foreach (Types.AvailablePlugin pluginOn in this.List)203				{204					//Find the one with the matching name or filename205					if ((pluginOn.Instance.Name.Equals(pluginNameOrPath)) || pluginOn.AssemblyPath.Equals(pluginNameOrPath))206					{207						toReturn = pluginOn;208						break;		209					}210				}211				return toReturn;212			}213		}214		215		/// <summary>216		/// Data Class for Available Plugin.  Holds and instance of the loaded Plugin, as well as the Plugin's Assembly Path217		/// </summary>218		public class AvailablePlugin219		{220			//This is the actual AvailablePlugin object.. 221			//Holds an instance of the plugin to access222			//ALso holds assembly path... not really necessary223			private IPlugin myInstance = null;224			private string myAssemblyPath = "";225			226			public IPlugin Instance227			{228				get {return myInstance;}229				set	{myInstance = value;}230			}231			public string AssemblyPath232			{233				get {return myAssemblyPath;}234				set {myAssemblyPath = value;}235			}236		}237	}	238}239