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));
  }
}
Advertisements
This entry was posted in Brian By Experience. Bookmark the permalink.

One Response to Your own plugin lib for your .NET application

  1. Brian Webb says:

    Posted a correction due to my grabbing the wrong version of my code.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s