我有一个场景,我必须从我的 CompositionContainer 实例中获取导出,但我只有一个可以使用的类型;我在编译时不知道类型,因此我无法以正常的通用方式检索导出的对象。
通常你会这样做:
_container.GetExportedObject<IMyType>();
但就我而言,我有这个:
Type someType = ... ;
_container.HowDoIGetTheExport(someType);
有任何想法吗?
找到了答案:
var export = _container.GetExports(someType, null, null).FirstOrDefault();
使用 Type.MakeGeneric 动态创建调用.....
http://geekswithblogs.net/marcel/archive/2007/03/24/109722.aspx