From iLab Cookbook

CodeSnippets: RuntimeDllLoader

Created By: Rob Diaz-Marino

Date: Jan 27, 2010

Download: ModuleManager.cs

Purpose: At some point you may want to create a program that can handle an array of plugins. You want plugins able to be added or remove without requiring the original program to be recompiled. This means the plugins cannot be directly referenced by your main program, and must implement a common interface. Developers who want to create their own plugins for your program may do so by implementing your module interface and dropping the resulting DLL into the main program directory. Then your program, as it loads, can find all the plugins that are present, load them accordingly, and have their functionality available within your program.

Examples of programs that do this are:

Description: This file contains the code for a class that handles runtime DLL loading for your own plug-in modules. Each module must have one class that implements the IModule interface. The ModuleManager finds this class and stores an instance of it. This instance is used to dispatch further interfaces for your program to interact with your module, so design accordingly.

ModuleManager assumes all your module DLLs are in a single directory and are named using a particular pattern so that they can be distinguished from other support DLLs. It is built to expect that your program will store a list of known modules, however this can be optional. Notifications are thrown at each step of the module discovery process to offer your program (or your user) the chance to disable loading of any particular modules.

ModuleManager supports synchronous and asynchronous loading, however note that when using asynchronous loading and updating GUI components, you must use thread safe calling techniques to avoid illegal cross-thread operations. For your convenience, static ThreadSafeCall and ThreadSafeComponentCall methods are included in the ModuleManager class. These can be used to invoke methods or set properties on a target Control or Component.

Tutorials and Examples

ModuleManagerSample Project (

Retrieved from
Page last modified on February 03, 2010, at 02:47 PM