Click here to Skip to main content
11,574,494 members (52,836 online)
Click here to Skip to main content
Add your own
alternative version

Creating a Flexible Dynamic Plugin Architecture under .NET

, 11 Nov 2003 112.8K 3K 115
This article demonstrates how to create a simple class which can be extended to assist with plugin creation and management.
pluginmanager_demo.zip
Source
Interfaces
Interfaces.csproj.user
PluginSample
PluginSample.csproj.user
Widgets
Widgets.csproj.user
pluginmanager_src.zip
using System;

namespace PluginSample
{
	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			string Command = "";
			while (Command.ToLower() != "quit")
			{
				int WidgetCount = 0;
				System.Console.WriteLine("Widgets:\n-----------------------------------");
				foreach (string Widget in WidgetBroker.List())
				{
					System.Console.WriteLine(Widget);
					WidgetCount++;
				}
				System.Console.WriteLine("-----------------------------------\n{0} Widget(s) Found", WidgetCount);

				System.Console.WriteLine();

				System.Console.Write("\nWidget to Create (or 'Quit'): ");
				Command = System.Console.ReadLine();

				System.Console.WriteLine();

				if ((Command.ToLower() != "quit") && (Command.ToLower() != ""))
				{
					try
					{
						WidgetBroker.Load(Command).Go();
					}
					catch
					{
						System.Console.WriteLine("[Widget Not Found]");
					}
				}

				System.Console.WriteLine();
			}
		}
	}


	class WidgetBroker : Plugins.PluginLoader
	{
		private WidgetBroker() { }

		public static Widgets.IWidget Load(string name)
		{
			Type InputElementClass = FindAssembly("widget", name, System.IO.Path.Combine(Environment.CurrentDirectory, "plugins"), typeof(Widgets.IWidget));
			if (InputElementClass == null)
				throw new Exception("Type Not Found");

			return InputElementClass.GetConstructor(System.Type.EmptyTypes).Invoke(System.Type.EmptyTypes) as Widgets.IWidget;
		}

		public static string[] List()
		{
			Type[] TypeList = FindAllAssemblies("widget", System.IO.Path.Combine(Environment.CurrentDirectory, "plugins"), typeof(Widgets.IWidget));
			string[] Result = new string[TypeList.Length];
			for (int I = 0; I < TypeList.Length; I++)
				Result[I] = TypeList[I].ToString();

			return Result;
		}
	}
}

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

Share

About the Author

Paul Hounshell
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 12 Nov 2003
Article Copyright 2003 by Paul Hounshell
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid