Help getting started with MEF
Asked Answered
P

5

13

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

Pahoehoe answered 24/5, 2010 at 17:29 Comment(1)
What MEF does and what DI frameworks do aren't quite the same thing (though there is overlap, but your question may not be tagged correctly). There's a good article on how to make them place nice here blogs.msdn.com/nblumhardt/archive/2009/03/16/…Turgot
P
6

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.

Papke answered 24/5, 2010 at 17:52 Comment(1)
Excellent...thanks. For anyone else, they might find this link more helpful ... its a little more up to date. goo.gl/4jpDPahoehoe
D
3

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

Dominik answered 25/5, 2010 at 7:48 Comment(0)
S
2

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);
Steelmaker answered 27/5, 2010 at 17:45 Comment(0)
B
1

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

Bradawl answered 28/5, 2010 at 0:32 Comment(0)
D
1

This article provides an easy a generic approach of working with MEF:

  1. The Simplest way to (generically) use MEF.
  2. How to use MEF fully lazy also for the Dll loading process.
Dyane answered 27/11, 2012 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.