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

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

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(); 

But in my case, I have this:

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

Any ideas?

回答1:

Found the answer:

var export = _container.GetExports(someType, null, null).FirstOrDefault(); 


回答2:

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

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!