Your own plugin lib for your .NET application

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));
  }
}

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.