views:

306

answers:

4

Hi, I was reading somewhere that with MEF I can simply drop a dll into a directory and my application (with some MEF magic) will be able to read it and execute the code in it? Hopefully only classes that implement an interface that I define??

Can someone help me to get going, with some links maybe for my problem.

I've looked through some of the docs, but nothing seems to be what I'm after and its tricky when I don't know exactly what to search on...

Thx S

+4  A: 

Here are two MEF "Getting Started" posts by Brad Abrams:

Note that these were written using preview versions of MEF, so there have been some changes. For example, AttributedAssemblyCatalog has been renamed to AssemblyCatalog, AggregatingComposablePartCatalog is now AggregatingCatalog, and the PackageCatalog on Silverlight is now DeploymentCatalog, and has had some other API changes.

Daniel Plaisted
Excellent...thanks.For anyone else, they might find this link more helpful ... its a little more up to date. http://goo.gl/4jpD
SteveCl
+2  A: 

Try reading Glenn Block's introduction to MEF in MSDN Magazine:

Managed Extensibility Framework: Building Composable Apps in .NET 4 with the Managed Extensibility Framework

Mark Seemann
+2  A: 

You can support 'Recomposition' by marking the imports like:

[ImportMany(AllowRecomposition=true)]
public IMessageSender[] Senders { get; set; }

However, from what I can tell this doesn't automatically load the assemblies. The version of MEF that made it to .NET 4 (and I guess Preview 9 at complex) doesn't appear to load the assemblies automatically. I'm not sure if this behaviour changed as MEF was developed.

You'll need to add a FileSystemWatcher and call Refresh() on for example the DirectoryCatalog and listen for one or more these events:

catalog.Changed += new EventHandler<ComposablePartCatalogChangeEventArgs>(Catalog_Changed);
container.ExportsChanged += new EventHandler<ExportsChangeEventArgs>(Container_ExportsChanged);
directoryCat.Changed += new EventHandler<ComposablePartCatalogChangeEventArgs>(dCat_Changed);
Tim
+1  A: 

Here is a demo application built with WPF, MVVM, and MEF (for both composition and extensibility).

Scott Whitlock