Click here to Skip to main content
13,453,456 members (42,708 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 27 May 2013

MEF organization

, 28 May 2013
Rate this:
Please Sign up or sign in to vote.
Setting up MEF to load plugins from a directory.


There are tons of articles about MEF, yet it was hard to grasp the relationship between cogs in its machine. Even MSDN prefers to show some code instead of describing parts a bit. Here’s how I understand the organization of MEF pieces.

The big picture

  • AggregateCatalog is a collection of ‘Catalogs’, each indicating how/where MEF finds plugins. There are more catalogs than those I draw here.
  • CompositionBatch is a collection of ‘Parts’ each indicating what it exports or imports (of our plugin contract) and how it does it. There are different types of 'export' and 'import' like ImportMany or Lazy import very well described by MSDN already.
  • Container uses the ‘AggregateCatalog’ to find plugins that have any of the specified ‘parts’ of the ‘Batch’.


In below C# code PluginManager is the only ‘part’ in which I want to ‘Import’ any available instances of IMyContract and I want MEF to search for me the directory “D:\MyAppDir\Plugins\” on my hard disk.

public class PluginManager
    private List<IMyContract> _plugins;

    public void Setup()
        _plugins = new List<IMyContract>();

        var aggregate = new System.ComponentModel.Composition.Hosting.AggregateCatalog();

        aggregate.Catalogs.Add(new System.ComponentModel.Composition.Hosting.DirectoryCatalog(
            @" D:\MyAppDir\Plugins\"));

        var parts = new System.ComponentModel.Composition.Hosting.CompositionBatch();

        var container = new System.ComponentModel.Composition.Hosting.CompositionContainer(aggregate);

Now we can access our methods inside plugins like this: _plugins[0].Method1(); considering that at least one plugin is being found and loaded by MEF. 

Code of the export side (my plugin) is a class (inside a separate assembly) which looks like this: 

public class MyPlugin : IMyContract
    public string Name { get; set; }

It seems that not all types of classes can be a Part class. I tried it with a class derived from another class and parts.AddPart failed.  

The only needed reference is to System.ComponentModel.Composition and MEF is now part of the .NET Framework so nothing else is needed to be installed for MEF to work correctly on destination.


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


About the Author

Hamed Mosavi
Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...


Comments and Discussions

Questionperfect ;) Pin
Ghasrfakhri27-May-13 10:30
memberGhasrfakhri27-May-13 10:30 
AnswerRe: perfect ;) Pin
Hamed Mosavi27-May-13 18:12
memberHamed Mosavi27-May-13 18:12 

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
Web02-2016 | 2.8.180318.3 | Last Updated 28 May 2013
Article Copyright 2013 by Hamed Mosavi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid