Working with AssemblyCatalog in MEF

MEF is a component defined in Base class library for extensibility. If you are really looking for writing something that extend itself at runtime, or that supports plugins to be attached to it, you should give MEF a try. You can read how MEF works from my post here. In this post I will demonstrate how to use AssemblyCatalog in MEF World.

A Catalog is a container that lists Parts where each Part is actually mapped to a Type which individually hosts a number of Exports in forms of ExportDefination and number of Import in forms of ImportDefinations. An AssemblyCatalog is actually a collection of TypeCatalogs where the Types are actually defined within one particular assembly.

Lets take a look at AssemblyCatalog using our previous code :

public class ExportContainer
{
[Export]
public string ExportName { get; set; }

[Export]
public string GetName()
{
return this.ExportName;
}

[Export]
public Action MyActionDelegate { get; set; }

}

Now if I use Assemblycatalog to load the Catalog into Parts we write :

static void Main(string[] args)
{
AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

Console.WriteLine(catalog.Parts.Count());

Console.ReadKey(true);
}

Here the parts show up one Part with all the ExportDefinations in it. You can get the underlying Assembly in the catalog using a property Assembly in the catalog too.
Download sample code from here.

Read more about it from the links :

Managed Extensibility Framework – A Look

Steps to write a plugin based application with MEF

Thank you for reading.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434