Click here to Skip to main content
13,449,298 members (38,992 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 20 Jun 2014

Basics of Managed Extensibility Framework (MEF)

, 3 Jul 2014
Rate this:
Please Sign up or sign in to vote.
This article explains the basics of MEF for beginner with a simple examble


The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It is a composition layer for .NET that improves the flexibility, maintainability and testability of large applications; it is loosely couple plugin architecture and easily encapsulates code.

MEF is an integral part of the .NET Framework 4, and is available wherever the .NET Framework is used. You can use MEF in your client applications, whether they use Windows Forms, WPF, or any other technology, or in server applications that use ASP.NET.

Key Concepts

  • Export
    • Export attribute decoration will introduce the class to container, any export declared with a matching contract will fulfil this import from the container.
  • Import
    • Import attribute decoration will extract the classes from the container. Every import has a contract, which determines what exports it will be matched.
  • Contract
    • The contract can be an explicitly specified string, or it can be automatically generated by MEF from a given type.
  • Parts
    • All import & exportable components are commonly known as Parts.
  • Composition
    • The core of the MEF composition model is the composition container, which contains all the parts available and performs composition. CompositionContainer discover the parts using Catalog. A catalog is an object that makes available parts discovered from some source. MEF provides catalogs to discover parts from a provided type, an assembly, or a directory.
    • Based on the discovery catalog has been classified as
      • TypeCatalog - Discovers attributed parts from a collection of types.
      • AssesmblyCatalog - Discovers attributed parts in a managed code assembly.
      • DirectoryCatalog - Discovers attributed parts in the assemblies in a specified directory.
    • AggregateCatalog - An aggregate catalog that combines multiple catalogs    
  • System.ComponentModel.Composition
    • This .Net Framework reference is provides above MEF implementation classes.


This code samples explain very simple MEF implementation, this demo application will display message in console output and window message box output.

MEFDemo solution has four projects:

  1. MEFDemo - This project discover the exportable parts using CompositionContainer and call the ShowMyMessage method
  2. MEFDemo.Interfaces - This project has simple interface IMyMessage with a method declaration ShowMyMessage
  3. MEFDemo.ConsoleMessage - This project has console output implementation of IMyMessage
  4. MEFDemo.WindowsMessage - This project has Windows Message Box output implementation of IMyMessage 

In this example DirectoryCatalog is used for discover the exportable parts from the directory DLLs (folder in the application root path) with the help of below code.

    catalog.Catalogs.Add(new DirectoryCatalog(
                (new DirectoryInfo(
                    Path.Combine(Directory.GetCurrentDirectory(), "../../../DLLs"))

TypeCatalog & AssemblyCatalog can be used for discovering the exportable parts.

Below code extract the IMyMessage contract implemented classes from the container.

    var myMessages = _container.GetExportedValues<IMyMessage>();


IMyMessage interface has method declaration of ShowMessage, please see below code.

    public interface IMyMessage
        void ShowMyMessage(string message);

This interface is implemented in Console and Windows message display.

Console & Windows Implementation

IMyMessage interface implemented Console & Windows message for displaying difference output format of message.

    //This class can be export to CompositionContainer - If decorated with Export attribut
    // with IMyMessage as contract, then this class can be export to composition container
    // and then this can be import whenever it necessary from the container
    public class MyMessage : IMyMessage
        public void ShowMyMessage(string message)
            Console.WriteLine("Hi I am from {0}, {1}", "Console", message);


Same implementation for Windows Messagebox display, please see the code below

    public class MyMessage : IMyMessage
        public void ShowMyMessage(string message)
            MessageBox.Show(string.Format("Hi I am from {0}, {1}", "Window", message));


This ConsoleMessage and WindowsMessage are separate projects and there is no direct interact with the MEFDemo application.


By removing any one DLL from the below folder, corresponding message will not appear in the output.


I hope this article helps to understand some basics in MEF implementation & feel free to write your comments & question on this article.




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead
India India
Artha is a Technical Lead in Windows Phone, WPF [MVVM, PRISM], ASP.NET [MVC 3.0 & 4.0], C#.NET, VB.NET and ASP.

Windows Phone

Microsoft Developer Network

Published Tools in Visual Studio Galleries

Published Source Codes in CodePlex

Microsoft Virtual Academy Profile

Published Articles in Code Project

Microsoft Certified Professional Developer (MCPD) Microsoft ASP.NET Developer 3.5 (070-564, 070-536, 070-562, 070 315)

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
ady cody3-Jul-14 22:07
memberady cody3-Jul-14 22:07 
GeneralRe: My vote of 3 Pin
Arthanarieaswaran3-Jul-14 23:23
memberArthanarieaswaran3-Jul-14 23:23 
GeneralMy vote of 3 Pin
Klaus Luedenscheidt20-Jun-14 19:49
memberKlaus Luedenscheidt20-Jun-14 19:49 
GeneralRe: My vote of 3 Pin
Arthanarieaswaran24-Jun-14 20:02
memberArthanarieaswaran24-Jun-14 20:02 
GeneralMy vote of 1 Pin
Tim Kemp20-Jun-14 3:16
memberTim Kemp20-Jun-14 3:16 
GeneralRe: My vote of 1 Pin
Arthanarieaswaran24-Jun-14 20:02
memberArthanarieaswaran24-Jun-14 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180318.3 | Last Updated 4 Jul 2014
Article Copyright 2014 by Arthanarieaswaran
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid