MEF stands for Managed Extensibility Framework. It is now included in .Net 4.0.
Here I show you a simplest example to create a plugin to load modules dynamically.
Step 1. Design your interface. For example:
Step 2. Implement the interface, and "export" the class.
a. Add reference to System.ComponentModel.Composition
b. Add using System.ComponentModel.Composition;
c. Create a class that implement the interface and add Export attribute. For example:
Step 3. "Import" the class.
a. Create a Catalog. Since plug-ins usually implemented as separate DLLs, let's use DirectoryCatalog for this case.
b. Create the CompositionContainer with the catalog.
c. Use ImportMany to import a list of the classes.
d. Do ComposeParts() for the class you want to import the plugins.
e. In case, some user dropped in a new DLL, you can do Catalog.Refresh() to refresh the list of plugins.
For example:
It's simple. Isn't it?
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment