i’m new in the plugin world and force to unterstood mef.
Currently i know how i can load plugins from a specific folder.
Now i have some questions:
- Can i load plugins from a folder which are specify in a configuarations file (Serialization) ?
- How i can unload plugins ?
- How can i load plugins with a specific version between min&max version?
- How i can wrap the catalog & compose e.g. as something like “MEF Manager”
optionaly:
- How i can display forms from plugin?
thanks for help
Yes you can load plugins from folders using MEF’s DirectoryCatalog class. You will have to add you own section in the configuration file though and create a
DirectoryCatalogfor each folder where plugins are located. Unraveling the Mysteries of .NET 2.0 Configuration contains great information on how to use the classes in theSystem.Configurationnamespace.This depend on wht you mean by unloading a plugin.
Unloading plugins when an assembly is removed from a folder can be done using the
AllowRecompositionproperty of the import attributes of MEF and a FileSystemWatcher for each plugin folder to monitor deleted .dll files and call theDirectoryCatalog.Refreshmethod to force an update on the MEF container. Note though that by default you cannot delete assemblies that are loaded by .NET. To overcome this .NET supports Shadow Copying. Have a look at the The Way of MEF code by Glenn Block. ThePartUpdatesInPlaceis a good example on how to do this.Unloading plugins without the filesystem is a different thing. You will have to do this yourself. Usually you will need a plugin manager that can unload plugins at will.
Note though that the loaded assemblies will not be unloaded only the plugins.
You can use MEF’s Export Metadata to add plugin metadata. Then you the GetExports method of the CompisitionContainer and check the metadata before accesing the actual plugin. This is standard procedure with MEF-based plugin solutions. Also have a look at the ExportFactory class that was added in MEF2 for a better approach.
Start by a very simple plugin manager class that provides the basics (depend on what you want to do) and build from this.
You can do it in the same way as you would do it without MEF. For example you can add a form property to your plugin inteface/base class and make each plugin responsible for creating/disposing it.
Before you start doing all this I would advise you to check
Prism(Composite Application Guidance) andSmart Client Software Factory. These are powerful frameworks for WPF and WinForms applications that need great flexibility. If you decide to check them out I think the best way is to play around with samples.Good luck