12

我有一个场景,我必须从我的 CompositionContainer 实例中获取导出,但我只有一个可以使用的类型;我在编译时不知道类型,因此我无法以正常的通用方式检索导出的对象。

通常你会这样做:

_container.GetExportedObject<IMyType>();

但就我而言,我有这个:

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

有任何想法吗?

4

2 回答 2

22

找到了答案:

var export = _container.GetExports(someType, null, null).FirstOrDefault();
于 2009-06-24T12:02:31.670 回答
1

使用 Type.MakeGeneric 动态创建调用.....

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

于 2009-06-24T11:38:31.623 回答