Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an export from an MEF container given only a Type instance

Tags:

c#

mef

I have a scenario where I have to get an export from my CompositionContainer instance but I only have a Type to work with; I don't know the type at compile time, hence I can't retrieve the exported object in the normal generic way.

Normally you would do this:

_container.GetExportedObject<IMyType>();

But in my case, I have this:

Type someType = ... ;
_container.HowDoIGetTheExport(someType);

Any ideas?

like image 896
Nathan Ridley Avatar asked Jun 24 '09 11:06

Nathan Ridley


2 Answers

Found the answer:

var export = _container.GetExports(someType, null, null).FirstOrDefault();
like image 116
Nathan Ridley Avatar answered Sep 22 '22 05:09

Nathan Ridley


Create the call dynamically using Type.MakeGeneric.....

http://geekswithblogs.net/marcel/archive/2007/03/24/109722.aspx

like image 30
Preet Sangha Avatar answered Sep 22 '22 05:09

Preet Sangha