Click here to Skip to main content
15,893,722 members
Articles / Desktop Programming / Windows Forms

Plug-ins in C# 2.0: Generics Enabled Extension Library

Rate me:
Please Sign up or sign in to vote.
4.67/5 (39 votes)
7 Nov 2007CPOL7 min read 92.8K   1.3K   139  
A follow up to my previous article, this article takes the plug-in concept and encapsulates it in a Generics enabled library, including support for source code compilation at runtime.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ExtensionManager;
using Common;


namespace HostApplication
{
	public partial class frmHost : Form, IHost
	{
		public frmHost()
		{
			InitializeComponent();
		}

		//Our manager object
		ExtensionManager<IExtension, IHost> manager = new ExtensionManager<IExtension, IHost>();		

		private void frmHost_Load(object sender, EventArgs e)
		{
			//Listen to the events
			manager.AssemblyFailedLoading += new ExtensionManager<IExtension, IHost>.AssemblyFailedLoadingEventHandler(manager_AssemblyFailedLoading);
			manager.AssemblyLoaded += new ExtensionManager<IExtension, IHost>.AssemblyLoadedEventHandler(manager_AssemblyLoaded);
			manager.AssemblyLoading += new ExtensionManager<IExtension, IHost>.AssemblyLoadingEventHandler(manager_AssemblyLoading);

			//Loads .cs, .vb, .js, and .dll extensions
			manager.LoadDefaultFileExtensions();

			//We need to add our library with the interfaces to the list
			manager.SourceFileReferencedAssemblies.Add("Common.dll");

			//Lookin in the AppPath\Extensions\ folder
			manager.LoadExtensions(Application.StartupPath.TrimEnd("\\".ToCharArray()) + "\\Extensions\\");

			//Loop through all the extensions
			foreach (Extension<IExtension> extOn in manager.Extensions)
			{
				//It needs to know we're the host!
				extOn.Instance.Host = this;

				//Create a new listitem
				ListViewItem lvi = new ListViewItem(extOn.Instance.Title);
				lvi.Tag = extOn.Instance; //Store our instance in the tag for easy retrieval later

				//Add to the list
				this.listExtensions.Items.Add(lvi);
			}
		}

		void manager_AssemblyLoading(object sender, AssemblyLoadingEventArgs e)
		{
			//Simply update status
			statusLabel.Text = "Loading: " + e.Filename;
		}

		void manager_AssemblyLoaded(object sender, AssemblyLoadedEventArgs e)
		{
			statusLabel.Text = "Loaded: " + e.Filename;
		}

		void manager_AssemblyFailedLoading(object sender, AssemblyFailedLoadingEventArgs e)
		{
			//Build an error message with specific compiler errors
			StringBuilder msg = new StringBuilder();
			msg.AppendLine("Failed Loading Extension: " + e.Filename + " of Type: " + e.ExtensionType.ToString());
			msg.AppendLine("Error Message: ");
			msg.AppendLine(e.ErrorMessage);
			msg.AppendLine(" ");
			msg.AppendLine("Compiler Errors: ");

			foreach (System.CodeDom.Compiler.CompilerError errorOn in e.SourceFileCompilerErrors)
				msg.AppendLine("  #" + errorOn.ErrorNumber.ToString() + " on Line: " + errorOn.Line.ToString() + " at Column: " + errorOn.Column.ToString() + " - " + errorOn.ErrorText);

			//Show the user
			MessageBox.Show(this, msg.ToString(), "Compilation Error", MessageBoxButtons.OK);
		}

		#region IHost Members
		public void HostStatus(string statusMessage)
		{
			//Change the status label
			this.statusLabel.Text = statusMessage;
		}
		#endregion

		private void listExtensions_DoubleClick(object sender, EventArgs e)
		{
			//Make sure an item is selected
			if (this.listExtensions.SelectedItems.Count > 0)
			{
				//Get the instance
				IExtension extInstance = (IExtension)this.listExtensions.SelectedItems[0].Tag;

				//If we have an instance, call process!
				if (extInstance != null)
					extInstance.Process();
			}
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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