This is the first posting on MEF, in which I will be explaining some things that I discovered while playing around with MEF. I should say that all the postings on my blog, including this subject do not represent my employer or any other organization, they are merely my mumblings based on my work that I do and whatever in find interesting and would like to share. Use it at your own risk, but it works on my machine.

To start with I would like you to read about the architecture of the MEF framework from the MEF Codeplex site.
In my MEF101, I present an example which shows how you can import composable parts from your assemble and let MEF compose and satisfy imports and enable you to come up with a simple interface made from different parts.
I created the IPlugin interface which I use in my exports.
/// <summary>
/// IPlugin Interface
/// Used by all the composable parts which will be used as plugins
/// </summary>
public interface IPlugin
{
}
All rectangle items visible are simple UserControls which implement the IPlugin contract. Notice that I have placed an [Export] attribute. This informs MEF that during composition that if there is something which needs [Imports] IPlugin, then the MEF will create it and satisfy that import.
/// <summary>
/// Plugin1
/// Example Plugin
/// </summary>
[Export(typeof(IPlugin))]
public partial class Plugin1 : UserControl, IPlugin
{
public Plugin1()
{
InitializeComponent();
}
}
All the plugins in this example have this same structure. I just changed the background color in the designer to so that we can tell that they are different.
The Main form has the following code.
/// <summary>
/// Form1
/// The main form. We need to have the [Export] attribute on it because on the Program.cs
/// we compose it using MEF and get its instance as an ExportedObject.
/// </summary>
[Export]
public partial class Form1 : Form
{
/// <summary>
/// Plugins
/// These imports will be satisfied by instances of the IPlugin 's
/// which in this case is the user controls with different colors.
/// </summary>
[Import]
public IEnumerable<IPlugin> Plugins;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// We loop through all the instances of the plugins an add them to the tableLayoutPanel
// and because we know that all of them are UserControls we can cast the as Control.
foreach (var plugin in Plugins)
{
(plugin as Control).Dock = DockStyle.Fill;
tableLayoutPanel1.Controls.Add(plugin as Control);
}
}
}
The Program.cs has the following code.
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form1 = Compose();
Application.Run(form1);
}
public static Form1 Compose()
{
//Create a catalog - in this case we want to import the composable parts which exist in this
//assembly, so we will use the AssemblyCatalog against the current assembly.
//
//It is also possible to import assemblies from external assemblies using the DirectoryCatalog.
//Furthermore you can combine a number of catalogs into an AggregateCatalog.
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
//Create the Container - then pass the catalog into the container
var container = new CompositionContainer(catalog);
//Return the instance of Form1.
return container.GetExportedObject<Form1>();
}
}
And that’s all. Look at how we create the instance of Form1 class and use the Application class to run it.
You can download the source here