Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Generics » Downloads
 
Add your own
alternative version

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

, 7 Nov 2007 CPOL
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.
PluginsInCSharp2_examplesrc.zip
PluginsInCSharp2
HostApplication
ExtensionManager
Common
ExtensionOne
ExtensionTwo
ExtensionFour
ExtensionThree
bin
Properties
Release
HostApplication.exe
Common.dll
ExtensionManager.dll
Extensions
ExtensionOne.dll
ExtensionThree.dll
Settings.settings
bin
EventArgs
Release
ExtensionManager.dll
bin
Properties
Release
Common.dll
bin
Properties
Release
ExtensionOne.dll
Common.dll
bin
Properties
Release
ExtensionTwo.dll
Common.dll
bin
Properties
Release
ExtensionFour.dll
Common.dll
bin
Properties
Release
Common.dll
ExtensionThree.dll
PluginsInCSharp2_src.zip
ExtensionManager
bin
Release
ExtensionManager.dll
EventArgs
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)

Share

About the Author

Redth
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)

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 7 Nov 2007
Article Copyright 2007 by Redth
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid