This was explained in a previous post Generic base class for composing objects with MEF.
This works perfect when the Type is known a compile time. But what if we want to load objects that are defined in a config file? What if we want to create a IoC implementation with MEF where the proper instance is defined in a config file and can be set at runtime. This way the Type is not know at compile time even though the composition relies on knowing the type.
To achieve this there needs to be a ComposablePartCatalog that can compose objects depending on the type.
public class ComposeableTypeCatalog : ComposablePartCatalog { ComposablePartCatalog _catalog; public ComposeableTypeCatalog(ComposablePartCatalog catalog) { _catalog = catalog; } public override IQueryable<ComposablePartDefinition> Parts { get { return _catalog.Parts; } } public override IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExports(ImportDefinition importDef) { var result = new List<Tuple<ComposablePartDefinition, ExportDefinition>>(); // check all parts in that catalog foreach (ComposablePartDefinition partDef in Parts) { // for each part, examine if any export definition matches the requested import definition. foreach (ExportDefinition exportDef in partDef.ExportDefinitions) { // the import has to be made on object if(Type.GetType(importDef.ContractName) == typeof(object)) { var matchingExport = new Tuple<ComposablePartDefinition, ExportDefinition>(partDef, exportDef); result.Add(matchingExport); } } } return result; } }
In the composition we need to use a TypeCatalog to define the type that should be composed. The big drawback with this approach ist the fact that the Imports have to be made on System.object or IEnumerable<System.object> and not on a concrete type.
public class TypeComposer { private CompositionContainer _container; [ImportMany] public IEnumerable<object> Imports { get; set; } public object Compose(Type type) { // an aggregate catalog that combines multiple catalogs var agregatecatalog = new AggregateCatalog(); // load only items of type agregatecatalog.Catalogs.Add(new TypeCatalog(type)); // create a catalog that loads the given types into the IEnumerable<object> var typecatalog = new ComposeableTypeCatalog(agregatecatalog); // create the CompositionContainer with the parts in the catalog _container = new CompositionContainer(typecatalog); // fill the imports of this object try { _container.ComposeParts(this); } catch (CompositionException compositionException) { Console.WriteLine(compositionException.ToString()); } if (Imports == null) return null; return Imports.FirstOrDefault(); } }This class now imports all objects that are marked with an ExportAttribute containing the type that is passed with the parameter.
[Export(typeof(ComposeableClass))] public class ComposeableClass { }
[TestMethod] public void TypeComposerTest() { var composer = new TypeComposer(); var part = composer.Compose(typeof(ComposeableClass)); Assert.IsNotNull(part as ComposeableClass); }As mentioned previously,the big drawback is the fact that the composition is made on System.object and not on the concrete type. This means the object has to be cast into the expected type which can lead to errors.