This project has moved and is read-only. For the latest updates, please go here.

How should I use Lazy constructor Func<T> in Container.GetExport method?

Nov 8, 2009 at 2:50 PM
Edited Nov 8, 2009 at 5:00 PM

How should I use Lazy constructor Func<T> in Container.GetExport method?


I have been looking for this issue a bit on lately, and I was wondering if this is something that will come later delivery of MEF or is this a part that must be implemented by each design?


Lazy allows passing constructor delegate into a lazy object as using Func<T>, for example:


// constructor for Lazy

Lazy<Customer> Consumer;

Consumer = new Lazy<Customer>(()=> new Customer(123));


I cannot find any Func<T> implementations in MEF Container GetExport methods that utilize the Lazy Func<T> constructor. I was expecting to see following Func<T> implementations on GetExport<T> methods:


GetExport<T> (Func<T> constructor)

GetExport<T> (string contractName, Func<T>  constructor).


Should I use other method than GetExport<T> to utilize Lazy Action constructor or shol I implement extension that utilizes the Func<T> in Lazy object?


Nov 9, 2009 at 5:11 PM

GetExport* is not a replacement for Activator.CreateInstance, nor is it meant to be a factory for a specific T. In fact T generally should not be a concrete implementation type and instead should be a abstract type, usually an interface. In which case you should never know what the constructor arguments are for implementation of T, in which case you can never actually provide them. So to answer your question you cannot provide constructor arguments to GetExport* and it is unlikely that we will support that in the future.

Now you can use the ImportingConstructorAttribute on the impelementation of T and import other value in the container.

Nov 9, 2009 at 8:00 PM
Edited Nov 10, 2009 at 3:30 PM

This thread is followed up in "Posted some code on using MEF to make extensible WPF apps with MVVM"


I constructed a small sample from proof of concept I have. Please see the sample at 

Please make the following notes:

I have not refractored this to interface but in proof of concept I have them in interface. Either way this works with the workaround I currently have.

This sample shows the customers, once you click "Show Orders" you get new view resolved by MEF and inserted into the current view. If you click the Order you see the order details.

My problem here is that the parent view is aware of child view. I havent be able to populate the child view information any other way by MEF. See the CustomersViewModel and ShowDetails method. Please note, even if I would refactor this to interface I would not be able to get rid of the coupling unless I am able to send some kind of parameters while MEF is resolving the export or constructing the child view in this case.  

Any thoughts and directions would be great… So far MEF has made this MVVM model much simpler than Unity and I would love to keep it in MEF as I would have smaller code base than with similar solution with Unity.