I have created a DLL loader for Control DLL's (these controls have the same interface iControlPlugin), all is working great. However I now need to create a DLL loader for device drivers. I started looking at my Control Loader code and realised that the code is identical, except where the interface type is defined. I would therefore like to make my control loader function more generic so that it can be used for different interface types.
I have modified the function so that it returns a dictionary, the problem I am having is that I do a check to make sure that the DLL file contains a matching type, in my case this is one of two interface types. But I always get null.
This is my original, working function which is specifically targeted at one type (iLPPControlPlugin)
public Dictionary<string, iLPPControlPlugin> LoadPlugins(string folder)
{
/* Create a Dictionary object to store the loaded DLL's */
Dictionary<string, iLPPControlPlugin> plugsins = new Dictionary<string,iLPPControlPlugin>();
/* Get all the DLL files from the path specified */
string[] files = Directory.GetFiles(folder, "*.dll");
/* Go through each DLL... */
foreach (string file in files)
{
/* Load the file, as an assembly. */
Assembly assembly = Assembly.LoadFile(file);
/* Get the type, can be more than one */
var types = assembly.GetExportedTypes();
/* For each type... */
foreach (Type type in types)
{
if (type.GetInterfaces().Contains(typeof(iLPPControlPlugin)))
{
/* Create an instance of the DLL */
object instance = Activator.CreateInstance(type);
/* As it is the correct type we know it will contain the "GetControlName" function.
* Call this to get the controls name. */
string controlName = ((iLPPControlPlugin)instance).GetControlName();
try
{
/* Try to add the instance to the dictionary list, using the control name as the key. */
plugsins.Add(controlName, instance as iLPPControlPlugin);
}
catch (ArgumentException)
{
/* Fails if there is already a DLL loaded with the same control name. This is a feature
* of the dictionary object. */
Console.WriteLine("Two or more Dll's have the same control name: " + controlName);
}
}
}
}
return plugsins;
}
I have therefore changed the function, to be more generic, to this:
public Dictionary<string, object> LoadPlugins(string folder, string interfaceType)
To simplify the problem I currently ignore the parameter "interfaceType" and have hard coded the string, and check to this:
Type myType = Type.GetType("PluginInterface.iLPPControlPlugin");
if (type.GetInterfaces().Contains(myType))
However the problem is that myType is always null. I have tried many different variations is no luck. I'm sure its going to be something simple but can anyone tell me what I am doing wrong. For information the PluginInterface namespace and iLPPControlPlugin class is a project within the solution.
Many thanks,
Chris