Working with AssemblyCatalog in MEF

By | August 30, 2011

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.