The nice thing about MEF is the ability to dynamically load additional modules in to an application with very little effort.
To make it even easier, I’ve generated a small MEF Compose which will load up all DLL in a directory automatically.
private void Compose()
{
var catalog = new AggregateCatalog(
new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()),
new DirectoryCatalog(@”.\Extensions”));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
The above code works well for .NET, however for some reason .NET 3.5 version, which is available from CodePlex just comes back with nulls, so here is a solution for MEF for .NET 3.5
private void Compose()
{
var key = Assembly.GetExecutingAssembly().GetName().GetPublicKey();
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var dir = new DirectoryInfo(@”.\Extensions”);
Assembly assembly;
foreach (var file in dir.GetFiles(“*.dll”))
{
assembly = Assembly.LoadFile(file.FullName);
byte[] assemblykey = assembly.GetName().GetPublicKey();
catalog.Catalogs.Add(new AssemblyCatalog(assembly));
}
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}