How can you add an already created instance to a MEF container/cataloge to use when resolving Imports. I want the functionality that Unity gives with the RegisterInstance
method on its containers.
Asked
Active
Viewed 7,230 times
16

Cornelius
- 3,526
- 7
- 37
- 49
-
MEF is not an IoC container, you shouldn't expect from MEF that it will replace Unity as it is not made for it. You can read about it: http://www.google.pl/search?sourceid=chrome&ie=UTF-8&q=mef+as+ioc – NOtherDev Mar 04 '11 at 08:33
-
2@A. I do not expect that it will replace Unity, but so far the project came along without including another IoC framework and this is the only thing that I need and does not justify adding another framework if it is possible to resolve this requirement with MEF. – Cornelius Mar 04 '11 at 08:39
-
MEF2 is a full fledged IOC. – stricq Feb 22 '15 at 19:28
1 Answers
23
You can use the ComposeExportedValue function for this, like so:
var container = new CompositionContainer();
container.ComposeExportedValue<Application>("Application", Application.Current);

alexn
- 57,867
- 14
- 111
- 145
-
1
-
1The other overload for ComposeExportedValue takes a string for the contractName. Updated my example. – alexn Mar 04 '11 at 08:38
-
1