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:
optionaly:
thanks for help
Can i load plugins from a folder which are specify in a configuarations file (Serialization) ?
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 DirectoryCatalog
for each folder where plugins are located. Unraveling the Mysteries of .NET 2.0 Configuration contains great information on how to use the classes in the System.Configuration
namespace.
How i can unload plugins ?
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 AllowRecomposition
property of the import attributes of MEF and a FileSystemWatcher for each plugin folder to monitor deleted .dll files and call the DirectoryCatalog.Refresh
method 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. The PartUpdatesInPlace
is 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.
How can i load plugins with a specific version between min&max version?
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.
How i can wrap the catalog & compose e.g. as something like "MEF Manager"
Start by a very simple plugin manager class that provides the basics (depend on what you want to do) and build from this.
How i can display forms from plugin?
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) and Smart 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