The code sample below assumes that you have your interface class wrapped in another assembly. And that your DLLs are based on that assembly.
If anything varies from this, then the code below will not work.
Header
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection;
In your main method.
string appPath = Path.GetDirectoryName(Application.ExecutablePath); string filePath = System.IO.Path.Combine(appPath,"Plugins"); //Directory of where your plugins are stored. string[] dllList = Directory.GetFiles(filePath); foreach (string dll in dllList) { Assembly o = Assembly.LoadFile(dll); List<Type> types = o.GetTypes().Where(n => n.GetInterface("IMyInterfaceClass") != null).ToList(); //Limit to only useful types. if (types.Count > 0) { MyDLLAssembliesList.Add((MyInterfaceAssembly.IMyInterface) o.CreateInstance(types[0].FullName)); } }
Posted a correction due to my grabbing the wrong version of my code.