6

I'm writing a simple plugin based program. I have an interface IPlugin which has some methods and functions, and a List<Plugin> in my main program. For the sake of simplicity, lets say its defined like this:

public interface IPlugin
{
    public void OnKeyPressed(char key);
}

Everytime a key is pressed, I loop through the Plugin list, and call OnKeyPressed(c) on each of them.

I can create a class like so, and add it to the list...

public class PrintPlugin
{
    public void OnKeyPressed(char key)
    {
        Console.WriteLine(c);
    }
}

And then whenever you press a key, its printed out. But I want to be able to load plugins from DLL files. This link was helpful, but it doesn't explain how to have the classes in the DLL implement my IPlugin interface... How can I do that? I really don't want to have to copy the IPlugin.cs file every time I want to make a plugin...

Community
  • 1
  • 1
Entity
  • 7,972
  • 21
  • 79
  • 122
  • You can add reference to your library, and since that you can use your plugin interface. – VMAtm Jul 18 '11 at 19:53
  • I want to be able to load them at runtime, so that the users can create their own plugins. – Entity Jul 18 '11 at 19:54
  • Then you should create a property like `List`, so users can add their plugins to your list. – VMAtm Jul 18 '11 at 20:01
  • I already have one of those, if you read the original post. My problem is, I want the plugin writers to be able to implement my `IPlugin` interface in their plugins. – Entity Jul 18 '11 at 20:07
  • 1
    You should share your IPlugin assembly to the `plugin writers`. – Tocco Jul 18 '11 at 20:12

3 Answers3

7

If I am understanding you correctly...

Create 3 Projects:

Project 1: Your main program (the one with List in it)

Project 2: the project with your interface

public interface IPlugin
{
public void OnKeyPressed(char key);
}

Project 3: A sample Plugin

public class PrintPlugin : IPlugin
{
public void OnKeyPressed(char key)
{
    Console.WriteLine(c);
}
}

Then Add project 2 as a reference to both project 1 and 3.

This way you share the interface with both your main project and any of your plugins.

I have used this on a couple of projects and it has served me well.

deepee1
  • 12,878
  • 4
  • 30
  • 43
2

You may want to look into the Managed Extensibility Framework as well. It provide a complete API for writing plugin based programs and covers a lot of concerns such as security if you're ever going to plan to make the plugin API available to third parties.

Steven Behnke
  • 3,336
  • 3
  • 26
  • 34
0

If you need to load user defined plugins, you should search for new DLLs when the application starts (or any other action). This can be done by using:

1) AppDomain.CurrentDomain.GetAssemblies() method returns the list of loaded assemblies in the current AppDomain

2) Search all DLLs in a folder where plugins should be positioned and check if a certain assembly is in the list. If not, use the Assembly.Load method to load this assembly, find the IPlugin class in it and finally add it to the List object.

platon
  • 5,310
  • 1
  • 22
  • 24