Not sure the exact terms to use here, so any help would be appreciated.

To be the most simplistic, I have a forms application. When I load "form.exe", the form has one control, a menu. This menu has one menuitem, "Exit". What I would like, is when the Form is loading, be able to pass in arguments to this forms app that specify DLLs that could add specialized methods, controls, etc.

Now when I load "form.exe add_plugable.dll", my menu has another menuitem, "Add". When I load "form.exe add_plugable.dll remove_pluggable.dll", menu has 3 items "Exit", "Add", and "Remove".

Hope this makes sense. What do I need to include in the Forms application and how do I create my DLLs? I am aware I need a common interface, but dont know exactly how to accomplish this with namespaces and interfaces and abstract classes.

Thanks!

有帮助吗?

解决方案

I would recommend looking into the Managed Extensibility Framework. It was designed for this exact type of scenario.

For example, it is what Visual Studio uses for its extensibility, so add ins can be dynamically discovered and plugged in at runtime.

其他提示

You can use a pre-written framework, but if you are looking for a "bare bones" solution, try this:

  1. Create an interface (or set of interfaces, if you prefer) that contains all methods that the forms app will call, e.g. IPlugin. Put the interface(s) in a separate class library.
  2. In each plugin DLL, create a public class that implements IPlugin.
  3. In the forms app, decide what plugins to load and when to call them. This logic is entirely up to you.

Load a plugin like this, then call the interface members to accomplish what you want. You could read the plugin file and class names from app.config, a database, or calculate them via a naming convention, according to your requirements.

var pluginAssembly = Assembly.Load(pluginFileName);
var plugin = (IPlugin) pluginAssembly.CreateInstance(pluginClassName);

These methods have several overloads allowing more control over the process. For example, you can load the plugin assembly using an AssemblyName rather than a simple string. When instantiating the class, you can specify binding flags and a set of parameter values to pass to the class's constructor.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top