Using CompositionContainer

How can I get Exports using a Type object?

CompositionContainer does not provide non-generic overloads of many of the GetExport* methods, e.g. GetExportedValue().

// not supported
object value = container.GetExportedValue(type);
' not supported
Dim value As Object = container.GetExportedValue(type) 

Instead, use the GetExports(ImportDefinition) overload:

IEnumerable<Export> matching = container.GetExports(

  new ContractBasedImportDefinition(








object value = matching.Single().Value;
Dim matching As IEnumerable(Of Export) = container.GetExports(
    New ContractBasedImportDefinition(
        Enumerable.Empty(Of String)(), 
Dim value As Object = matching.Single().Value

How do I use Nested Containers?

Nested containers are sometimes used as a convenient way to manage lifetime and sharing scope.

The best available source of information discusses filtered catalogs: Filtering Catalogs.

When child containers are created, they're typically given two constructor parameters - their own catalog, and their parent (a CompositionContainer passed as an ExportProvider.)

Using this configuration, all dependencies are requested via the most-nested child container.

If a dependency can be satisfied from the container's own catalog, it will do so. The lifetime of any resulting part will be tied to the lifetime of the child container, and all of that part's dependencies will be satisfied from the same container (recursively, using this algorithm.)

When a child container can't satisfy a dependency via its own catalog, it calls into its parent. If a dependency is retrieved from the parent this way, the lifetime of the resulting part will be tied to the parent. Any dependencies that part has will also be satisfied by the parent, never calling back into the child.

The key to setting up the whole configuration successfully is the catalog provided to each container.

As a general rule, any part that is NonShared should be able to be created at any level, and so should appear in all the containers' catlogs.

In a hierarchy of containers, Shared parts are a bit trickier. Sharing is related to the container in which the part is created, so a Shared part in a child container's catalog will be shared within that specific child container; other containers that can see the same part in their catalogs will create and share their own instances.

This means that within a single object graph, it may be possible to navigate to more than one instance of a Shared part (e.g. one in the child, another accessible through a dependency resolved in the parent.)

Last edited Aug 9, 2010 at 5:46 PM by haveriss, version 2


tilovell Apr 27, 2010 at 9:37 PM 
Q: Can I export a [delegate to a] static function on a static class as a Part?

gblock Mar 27, 2010 at 10:45 PM 

MEF does not support open generic parts out of the box. There is an implementation for Open Generics including in the MEF contrib ( project that you may want to check ouot.

GProssliner Jan 1, 2010 at 8:15 PM 
Is there a way for MEF to import Classes with Type-Parameters? like:

interface IContract{ void foo();}

abstract class Base<T> : IContract { }

class C1 : Base<int>{}
class C2 : Base<string>{}
class C3<T>: Base<T> {}

If I use
[ImportMany]IEnumerable<IContract> Implemenations {get;set;}
MEF will return my an instance of C1 and C2, because it knows how to create an instance of them. But how can I get C3? Of cause I can only get it if I know the Type of 'T'. Like:

var r = container.GetGenericExport<IContract, bool>(); // here I'd like to get C3 with an 'bool' as T.

Is there a way to implement such a design?

Thanks, GP