tags:

views:

284

answers:

2

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?

A: 

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

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

Preet Sangha
That doesn't help me call a generic method on the container... I don't think you read my question properly.
Nathan Ridley
I see you have a specific answer - excellent. My answer is more general in the sense that from a Type you can construct a generic method call at run time. We use this to construct calls where only interfaces are know at compile time but running instances can be fed from external sources.
Preet Sangha
+1  A: 

Found the answer:

var export = _container.GetExports(someType, null, null).FirstOrDefault();
Nathan Ridley
Yes that is the core method that most of the generic overloads use, so that is the best way to get it.
Wes Haggard