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
15
votes
2 answers

SatisfyImportsOnce vs ComposeParts

Can someone please explain the difference between SatisfyImportsOnce and ComposeParts and why one would work where the other doesn't? Specifically I have a MVC Web application that I am using MEF in. Below is some code (from that application) that…
Peter
  • 7,792
  • 9
  • 63
  • 94
15
votes
2 answers

Extending the Visual Studio 2010 editor by adding a context menu item to manipulate selected text

I'd like to create an extension to Visual Studio that will allow someone to select any text in an editor, right-click to get a context menu, and then perform some action on the text by clicking on my custom menu item. Let's say for example that my…
Andy West
  • 12,302
  • 4
  • 34
  • 52
15
votes
2 answers

Mono and MEF Are they compatible.

The two M's. Are they compatible? I would like to use MEF, or not, depending if it can run in MONO.
scope_creep
  • 4,213
  • 11
  • 35
  • 64
15
votes
1 answer

How to export & import functions and execute them with MEF?

I am creating an application that imports several plugins. I need the ability to execute functions that are implemented in each of the plugins. For example, I need to do something like this. …
John_Sheares
  • 1,404
  • 2
  • 21
  • 34
15
votes
5 answers

Using MEF as an IoC

After reading some stuff such as this: http://mikehadlow.blogspot.com/2008/09/managed-extensibility-framework-why.html I understand that MEF has some featcures that I will not find in an IoC, and that MEF has some IoC stuff that is probaboly not as…
Asher
  • 151
  • 1
  • 4
15
votes
1 answer

MEF: Where should I put the CompositionContainer?

I have been using the Windsor IoC Container for my web-based application, to resolve the data access layer implementation the application should use. The web application's UI will consist of pages, and each page consists of small units called…
Venemo
  • 18,515
  • 13
  • 84
  • 125
15
votes
4 answers

When to use Weak Events?

I was refering MSDN tutorial on weak events. I understood the basics. I am working on a non-WPF project and my class is exposing certain events. My question is that does the weak events completely replace old event pattern? Is it good to use it…
logeeks
  • 4,849
  • 15
  • 62
  • 93
15
votes
2 answers

What is .NET Portable Subset (Legacy)?

Object browser in Visual Studio 2012 offers two different component sets for Portable class libraries: .NET Portable Subset .NET Portable Subset (Legacy) When I create Portable Class Library it uses .NET Portable Subset. What is the second set and…
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
15
votes
3 answers

Visual Studio Editor Extension Options Dialog

I have a simple Visual Studio extension that is built in a similar manner as the one presented in this walkthrough (using the IWpfTextViewCreationListener interface). The extension uses two colors that I'd like to make configurable. How can I define…
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
14
votes
1 answer

export generics in MEF

I want to export a generic class to a generic interface via MEF. My objects are: public interface IService { } [Export(typeof(IService))] // error!!!!!! public class Service { } public class Client { [Import] private…
amiry jd
  • 27,021
  • 30
  • 116
  • 215
14
votes
4 answers

MEF keeps reference of NonShared IDisposable parts, not allowing them to be collected by GC

I've encountered somewhat of a problem in MEF's part lifetime which causes memory leaks in my Prism application. My application exports views and viewmodels with the PartCreationPolicy being set to CreationPolicy.NonShared. The views and viewmodels…
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
14
votes
4 answers

MEF recursive plugin search

Let's say that I have a few applications in a folder (each application has subfolders where plugins can be located): Clients Application A ... Application B ... Application C ... ... Some files in these applications have an…
Bram W.
  • 1,587
  • 4
  • 16
  • 39
14
votes
3 answers

Methods for composing configuration for composite applications (eg PRISM, MEF)

Frameworks such as PRISM and MEF make it very easy to design complex applications out of multiple, composable components. One common example of this is a plug-in architecture where an application shell can be dymanically reconfigured with plug-in UI…
Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
14
votes
1 answer

How to set ExportMetaData with multiple values as well as single w/ custom attribute?

I have the following ExportMetaData attributes set on my class: [Export(typeof(IDocumentViewer))] [ExportMetadata("Name", "MyViewer")] [ExportMetadata("SupportsEditing", true)] [ExportMetadata("Formats", DocFormat.DOC, IsMultiple = true)] …
Matthew M.
  • 3,422
  • 4
  • 32
  • 36
14
votes
3 answers

New Prism Project - Use MEF or Unity?

I'm starting a new personal Prism 4 project. The Reference Implementation currently uses Unity. I'd like to know if I should use MEF instead, or just keep to Unity. I know a few discussions have mentioned that these two are different, and they do…
Jonas Arcangel
  • 2,085
  • 11
  • 55
  • 85