Catalogs
One of value propositions of MEF's attributed programming model is the ability to dynamically discover parts via catalogs. Catalogs allow applications to easily consume exports that have self-registered themselves via the Export attribute. Below is a list the catalogs MEF provides out of the box.
Assembly Catalog
To discover all the exports in a given assembly one would use the
[System.ComponentModel.Composition.Hosting.AssemblyCatalog].
var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
Directory Catalog
To discover all the exports in all the assemblies in a directory one would use the
[System.ComponentModel.Composition.Hosting.DirectoryCatalog].
var catalog = new DirectoryCatalog("Extensions");
If a relative directory is used it is relative to the base directory of the current AppDomain.
The DirectoryCatalog will do a one-time scan of the directory and will not automatically refresh when there are changes in the directory. However, you can implement your own scanning mechanism, and call
Refresh() on the catalog to have it rescan. Once it rescans, recomposition will occur.
var catalog = new DirectoryCatalog("Extensions");
//some scanning logic
catalog.Refresh();
Aggregate Catalog
When AssemblyCatalog and DirectoryCatalog are not enough individually and a combination of catalogs is needed then an application can use an
[System.ComponentModel.Composition.Hosting.AggregateCatalog]. An AggregateCatalog combines multiple catalogs into a single catalog. A common pattern is to add the current executing assembly, as well as a directory catalog of third-party extensions. You can pass in a collection of catalogs to the AggregateCatalog constructor or you can add directly to the Catalogs collection i.e. catalog.Catalogs.Add(...)
var catalog = new AggregateCatalog(
new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()),
new DirectoryCatalog("Extensions"));
Type Catalog
To discover all the exports in a specific set of types one would use a
[System.ComponentModel.Composition.Hosting.TypeCatalog].
var catalog = new TypeCatalog(typeof(type1), typeof(type2), ...);
Using catalog with a Container
To use a catalog with the container, simpy pass the catalog to the container's constructor.
var container = new CompositionContainer(catalog);