Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

Plug-ins in C#

Rate me:
Please Sign up or sign in to vote.
4.84/5 (169 votes)
4 Mar 200410 min read 567.6K   13.8K   437  
Give your application Plug-in functionlity!
using System;
using PluginInterface;

namespace Plugin2
{
	/// <summary>
	/// Plugin2
	/// </summary>
	public class Plugin : IPlugin  // <-- See how we inherited the IPlugin interface?
	{
		public Plugin()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		
		//Declarations of all our internal plugin variables
		string myName = "Plugin2";
		string myDescription = "Have Fun with this one! Click Away!";
		string myAuthor = "Jon Dick";
		string myVersion = "1.0.1";
		IPluginHost myHost = null;
		System.Windows.Forms.UserControl myMainInterface = new ctlMain();
		
		/// <summary>
		/// Description of the Plugin's purpose
		/// </summary>
		public string Description
		{
			get {return myDescription;}
		}

		/// <summary>
		/// Author of the plugin
		/// </summary>
		public string Author
		{
			get	{return myAuthor;}
		
		}

		/// <summary>
		/// Host of the plugin.
		/// </summary>
		public IPluginHost Host
		{
			//This part is currently not really implemented
			/*
			Here's the scoop though... You can make the host program
			Implement this interface... this essentially gives you the ability
			to allow plugins to access some functionality of the host program.
			
			Example: an mp3 player.  If you had the IPluginHost interface as like:
			
			public interface IPluginHost
			{
				void Play(string FileName);
				void Stop();			
			}
			
			what you would do is when the plugin is loaded in the host (this would be
			in like the PluginServices.cs file in the AddPlugin() method) you would 
			set:
				newPlugin.Host = this;
				
			this would give the plugin a reference to the host... now since the plugin
			knows the host contains these methods, it can easily access them just like:
			
				this.Host.Play("C:\MyCoolMp3.mp3");
				
			and then they could go:
			
				this.Host.Stop();
				
			all this being from right inside the plugin!  I hope you get the point.  It 
			just means that you can indeed make your plugins able to interact with the 
			host program itself.  Let's face it.. It would be no fun if you couldn't do this,
			because otherwise all the plugin is, is just standalone functionality running
			inside the host program.. (of course there are cases when you can still accomplish
			many things without needing to allow the plugin to play with the host... for example
			you could have an spam filter, and have each plugin be a different filter... that would
			be pretty simple to make plugins for...
			
			so anyhow, that is what the host thingy is all aboot, eh!	
			
			*/
			get {return myHost;}
			set	{myHost = value;}
		}

		public string Name
		{
			get {return myName;}
		}

		public System.Windows.Forms.UserControl MainInterface
		{
			get {return myMainInterface;}
		}

		public string Version
		{
			get	{return myVersion;}
		}
		
		public void Initialize()
		{
			//This is the first Function called by the host...
			//Put anything needed to start with here first
		}
		
		public void Dispose()
		{
			//Put any cleanup code in here for when the program is stopped
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
Currently I'm an Oracle DBA for a School Board, having recently completed my undergrad at the University of Guelph with a Bachelor of Computing.

I obviously enjoy programming Smile | :)

Contact Me:
(MSN: jondick at gmail dot com)
(IRC: Dalnet: #c#, #asp.net, #vb.net)
(IRC: FreeNode: #linuxpeople)

Comments and Discussions