1

Basically I want to be able to write a dll for my application that I will be able to put in a specific folder using a specific name and at runtime have that dll loaded and subscribe to a specific event. As an example I have a simple Windows Form App with a single button on it. I want to be able to have a MessageBox displayed when the button click event takes place but I want the displayed message to be controlled by an external dll that is loaded at runtime. What would be the best way to accomplish this?

etoisarobot
  • 7,324
  • 13
  • 49
  • 79

2 Answers2

3

Create an interface that includes at least one method to handle the event (your application will have to reference the assembly in which this is defined):

public interface IEventHandler {
    void HandleEvent(object sender, EventArgs e);
}

Add a class to the dll you want to load at runtime that implements the interface:

public class ConcreteEventHandler: IEventHandler {
    public void HandleEvent(object sender, EventArgs e) {
        // do something here
    }
}

In your application, use reflection to load the dll and create an instance of your concrete handler (error checking omitted):

// The assembly name/location could be configurable
Assembly      assembly = Assembly.Load("MyAssembly.dll");
// The type name could be configurable
Type          type     = assembly.GetType("ConcreteEventHandler");
IEventHandler handler  = Activator.CreateInstance(type) as IEventHandler;

You can hook this handler up to whatever you want, e.g.:

MyButton.OnClick += handler.HandleEvent;
Jeff Sternal
  • 45,522
  • 6
  • 87
  • 118
  • Thanks. When I get to the last line of you sample code which I change from MyButton.OnClick += handler.HandleEvent to MyButton.Click += HandleEvent I am getting "Object Reference not set to an instance of an object" error on this line. What am I missing? – etoisarobot Nov 23 '09 at 18:57
  • You can ignore the above comment regarding the null reference error. I copied the dll to a sep dir to import at runtime and then continued to work in Visual Studio so the dll I was working on was not the dll that was working on. Duh. Thanks. You example was very helpful. – etoisarobot Nov 23 '09 at 19:54
  • That's one of the eternal pitfalls of loading 'pluggable' assemblies via reflection - if you think of a good way around it, let us know! :) The best I can recommend is to add code to check for everything and throw very specific exceptions, like: `if (! File.Exists(assemblyPath)) { throw new FileNoteFoundException(string.Format("Cannot locate assembly at {0}.", assemblyPath)); }`. (And add something similar for type, etc.) – Jeff Sternal Nov 23 '09 at 20:07
0

Is the DLL a single well-known one or will you be loading different or multiple ones? If the latter, I suggest looking into the AddIn framework in .NET 3.5.

Given either of those choices, a very good design pattern to employ in this case is the Observer pattern. Your "observers" are watching the button click event (the observed) and execute their MessageBox(es) appropriately.

Community
  • 1
  • 1
Jesse C. Slicer
  • 19,000
  • 3
  • 63
  • 80