colinramsay.co.uk

Simple C# Plugin Implementation - Code Challenge

19 Apr 2007

When building Wiretap, I wanted an easy way of adding new Test Types without having to do much work. It occurred to me that what I needed was to basically supporting Test Types as plugins, so I wrote a small method to achieve this. Here's the code, adapted for general use.

public IPlugin LoadPluginFromAssembly(string pluginAssembly, string pluginType)
{
	// This should come froma config file
	string sourceAssemblyPath = @"c:\plugins\" + pluginAssembly + ".dll";</code>

	// Load the dll containing containing the plugin type
	Assembly assembly = Assembly.LoadFrom(sourceAssemblyPath);

	Type testType = null;

	try
	{
		// Search all of the types until we find
		// one which matches the specified plugin type
		foreach (Type type in assembly.GetTypes())
		{
			if (type.IsClass)
			{
				if (type.FullName.EndsWith("." + testInstance.TestType.Type))
				{
					testType = type;
				}
			}
		}
	}
	catch (Exception ex)
	{
		_logger.Error(ex.Message);
	}

	return Activator.CreateInstance(testType) as IPlugin;
}

</code>

There are a number of improvements which I can see in this, so if you feel you can do better, post your own Better Plugin Implementation and link back here!

Feedback or questions on this post? Create an issue on GitHub.