Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help getting started with MEF

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

like image 861
SteveCl Avatar asked May 24 '10 17:05

SteveCl


3 Answers

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

  • Introduction to MEF (Desktop)
  • Introduction to MEF (Silverlight)

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.

like image 120
Daniel Plaisted Avatar answered Oct 31 '22 11:10

Daniel Plaisted


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

like image 3
Mark Seemann Avatar answered Oct 31 '22 12:10

Mark Seemann


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);
like image 2
Tim Avatar answered Oct 31 '22 11:10

Tim