D
Derrick
Hello all;
Since I do have working code, this is more for my curiosity only.
I'm creating a "Plugin" architecture, following some of the many examples on
the 'net. Basically what I have is this:
- a DLL which contains the interface that every plugin must implement
(IPlugin). This DLL also contains a class with the ability to search for
DLLs in the calling applications working directory that contain classes
implementing IPlugin (I call this the PluginLoader). I fixed the version of
this DLL to 1.0.0.0, so the version doesn't change everytime it's built.
- a DLL which contains a class that implements IPlugin, called MyPlugin.
This obviously references the IPlugin DLL.
- a test EXE which tries to load a plugin. Again, this obviously references
the IPlugin dll.
All the references are to the Projects within VS.NET, not to the DLLs.
As you can assume, there was a function in the PluginLoader as follows:
private Type[] SearchDirectoryForPlugins(string path)
{
string[] dlls = Directory.GetFiles(path, "*.dll");
ArrayList pluginList = new ArrayList();
foreach(string dll in dlls)
{
try
{
Assembly a = Assembly.LoadFile(dll);
Type[] types = a.GetTypes();
foreach(Type t in types)
{
if (t is FullNamespace.IPlugin)
{
pluginList.Add(t);
}
}
}
catch(Exception e) //should handle specific exception
{
Console.WriteLine(e.Message);
//the dll wasn't a valid .NET assembly. Ignore.
}
}
return (Type[]) pluginList.ToArray(typeof(Type)); //confusing, but I
like arraylists!
}
My problem was that the "if (t is FullNamespace.IPlugin)" line was returning
false when it encountered a class that does indeed implement IPlugin, and I
had no idea why. I have since changed the code, removing the "is", and now
using the Type.FindInterfaces function - which works perfectly.
Would anyone have any idea why "is" would not work as expected in this
scenario, when FindInterfaces works fine??
Thanks,
Derrick
Since I do have working code, this is more for my curiosity only.
I'm creating a "Plugin" architecture, following some of the many examples on
the 'net. Basically what I have is this:
- a DLL which contains the interface that every plugin must implement
(IPlugin). This DLL also contains a class with the ability to search for
DLLs in the calling applications working directory that contain classes
implementing IPlugin (I call this the PluginLoader). I fixed the version of
this DLL to 1.0.0.0, so the version doesn't change everytime it's built.
- a DLL which contains a class that implements IPlugin, called MyPlugin.
This obviously references the IPlugin DLL.
- a test EXE which tries to load a plugin. Again, this obviously references
the IPlugin dll.
All the references are to the Projects within VS.NET, not to the DLLs.
As you can assume, there was a function in the PluginLoader as follows:
private Type[] SearchDirectoryForPlugins(string path)
{
string[] dlls = Directory.GetFiles(path, "*.dll");
ArrayList pluginList = new ArrayList();
foreach(string dll in dlls)
{
try
{
Assembly a = Assembly.LoadFile(dll);
Type[] types = a.GetTypes();
foreach(Type t in types)
{
if (t is FullNamespace.IPlugin)
{
pluginList.Add(t);
}
}
}
catch(Exception e) //should handle specific exception
{
Console.WriteLine(e.Message);
//the dll wasn't a valid .NET assembly. Ignore.
}
}
return (Type[]) pluginList.ToArray(typeof(Type)); //confusing, but I
like arraylists!
}
My problem was that the "if (t is FullNamespace.IPlugin)" line was returning
false when it encountered a class that does indeed implement IPlugin, and I
had no idea why. I have since changed the code, removing the "is", and now
using the Type.FindInterfaces function - which works perfectly.
Would anyone have any idea why "is" would not work as expected in this
scenario, when FindInterfaces works fine??
Thanks,
Derrick