Questions tagged [mef]

The Managed Extensibility Framework (MEF) simplifies the design of extensible and modular applications, and is a standard component of Microsoft .NET 4.0 and Silverlight 4.0

MEF provides mechanisms for discovery and composition of modular component parts allowing a simple extension mechanism for .NET 4.0/Silverlight 4.0 applications. Through the use of the ExportProvider abstraction, MEF has the ability to easily discover and manage instances of your Exported types which easily allows you to bind to various extension points within your application.

Hello World Sample Using the default Attributed Programming Model, MEF allows you to decorate your target types with attributes which define contract definitions for how the part is composed. E.g.

public interface IMessage
{
  void Display();
}

[Export(typeof(IMessage))]
public class HelloWorldMessage : IMessage
{
  public void Display()
  {
    Console.WriteLine("Hello World");
  }
}

With that simple implementation, I am providing a an export definition of my HelloWorldMessage part, which I can use to compose my consumer:

public class Display
{
  [Import]
  public IMessage Message { get; set; }

  public void Display()
  {
    Message.Display();
  }
}

Using interfaces (contracts) this way allows me to maintain a decoupled implementation.

Make sure you visit the MEF CodePlex site for more detailed information works, and ensure you search Stack Overflow for possible answers before you post questions.

2226 questions
20
votes
2 answers

What is AggregateCatalog?

What is AggregateCatalog? What does it mean when you construct a new AggregateCatalog()? What does it mean when you add assemblies to the catalog, eg catalog.Catalogs.Add(new AssemblyCatalog(someAssembly))? Other than assemblies what can you add to…
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
20
votes
3 answers

MEF: "Unable to load one or more of the requested types. Retrieve the LoaderExceptions for more information"

Scenario: I am using Managed Extensibility Framework to load plugins (exports) at runtime based on an interface contract defined in a separate dll. In my Visual Studio solution, I have 3 different projects: The host application, a class library…
d7samurai
  • 3,086
  • 2
  • 30
  • 43
20
votes
2 answers

MEF: ComposeParts missing

I am trying to follow some starter guides for using MEF in .Net 4, but I get stuck when I get to setting up the application. The instructions say to do this: var catalog = new DirectoryCatalog(@".\"); var container = new…
Matt
  • 2,795
  • 2
  • 29
  • 47
20
votes
2 answers

How do I: Visual Studio Syntax Highlighting Extension

I want to develop an extension for VS2010 that will allow me make some additional features to syntax-highlighting. I installed the SDK, how do I start from? Please give a little snippet (or a link to code) where I can see how to start. Note: do I…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
20
votes
3 answers

How does MEF determine the order of its imports?

MEF allows you to import multiple parts via the use of the ImportMany attribute. How does it determine the order in which it retrieves the relevant exports and adds them to the enumerable you are populating? For example, how would I import multiple…
Luke Bennett
  • 32,786
  • 3
  • 30
  • 57
19
votes
1 answer

Alternatives to Prism + MEF for modular MVVM apps

My team and I are beginning to plan the development of a modular application which will likely multi-target WPF & Silverlight. I personally have some experience using the older version of PRISM to build a composite Silverlight app using the MVVM…
Steve Brouillard
  • 3,256
  • 5
  • 41
  • 60
18
votes
2 answers

How to remove MEF plugins at runtime?

I have a MEF-based application that can be customized with plugins. This application has several imported parts, and I want to remove some of them at runtime (to be able to delete the .dll that contains them) when a user decides to get rid of that…
Márton
  • 473
  • 7
  • 13
18
votes
4 answers

MEF vs. PRISM. What is the difference? What will be supported in the future?

What I want to create is a Silverlight app with a few tabs/modules that will all be separate DLLs. I see PRISM has the Shell/Module concepts that seem directed towards doing UI and I find a nice demo (showing how to search digg/twitter). But it…
punkouter
  • 5,170
  • 15
  • 71
  • 116
18
votes
1 answer

MVC4 MEF-based dynamically loaded plugins

updated: read below in this post for a minimal solution I have some newbie questions about an MVC4 solution with plugins. I googled around a bit and found some good stuff, but it does not exactly fit my requirements, so I'm asking here for some…
Naftis
  • 4,393
  • 7
  • 63
  • 91
17
votes
4 answers

Is MEF a replacement for System.Addin?

Possible Duplicate: Choosing between MEF and MAF (System.AddIn) Is the Managed Extensibility Framework a replacement for System.Addin? Or are they complementary?
Chris Sutton
  • 2,771
  • 5
  • 26
  • 32
17
votes
4 answers

Why use ImportingConstructor attribute?

I'm trying to understand when [ImportingConstructor] would be more appropriate than decorating properties with [import]. Is it a personal preference, or something that allows classes to be constructed by other DI containers or are there benfits over…
ILovePaperTowels
  • 1,445
  • 2
  • 17
  • 30
16
votes
1 answer

Adding an instance to a MEF container

How can you add an already created instance to a MEF container/cataloge to use when resolving Imports. I want the functionality that Unity gives with the RegisterInstance method on its containers.
Cornelius
  • 3,526
  • 7
  • 37
  • 49
16
votes
1 answer

Is there a replacement for MEF in .NET Core (or ASP.NET 5)

We know that .NET Core (the open-source components) are only a subset of the full .NET Framework, and that ASP.NET 5 (and MVC 6) is built on .NET Core. Does this mean that Managed Extensibility Framework (MEF) is not available in ASP.NET 5? If so,…
agc93
  • 663
  • 1
  • 7
  • 19
16
votes
1 answer

How can I write a plugin for VS2010 using MEF?

I've seen lots of MEF code for plugging into custom apps, but I am yet to find out how to write a plugin for VS2010 using MEF. I was under the impression that the new IDE supported this. Does anyone know if this is supported 'out of the box', or…
Ben Laan
  • 2,607
  • 3
  • 29
  • 30
15
votes
3 answers

How to use MEF Inherited Export & MetaData?

I have an interface: [InheritedExport(typeof(IMetric))] public interface IMetric { ... } I have a Meta attribute interface: public interface IMetricAttribute { ... } and an attribute that implements it: …
GreyCloud
  • 3,030
  • 5
  • 32
  • 47