OK, this is how I do it:
I check all files in a directory for .DLL or .EXE's. Then, I try to load each DLL. Then, I look at every Type in each DLL to see what it is. If it implements IPlugin (an interface I created that all my plugins or extensions follow), I load the object.
Code:
// here you could loop through a directory and try every file...
Assembly[] assemblies = new Assembly[1];
try
{
assemblies[0] = Assembly.LoadFrom(@"C:\firstPlugin.dll");
}
catch { }
try
{
assemblies[1] = Assembly.LoadFrom(@"C:\secondPlugin.dll");
}
catch { }
// search each loaded assembly for the type
foreach (Assembly assembly in assemblies)
{
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass)
{
// try to create an intance of it - maybe not the
// safest method but works
try
{
object o = Activator.CreateInstance(type);
// IPlugin is a custom interface we made
if (o is IPlugin)
{
IPlugin plugin = (IPlugin)o;
plugin.Execute();
}
}
catch
{
}
}
}
}