MEF:将类型与对象一起注入?
我想使用 MEF 构建一个可扩展的 NET 应用程序。这对于在我的应用程序中发现和注入新类型的实例非常有效。我遇到的问题是,我使用的某些 API 不允许注入新对象,而是接收插入对象的类型作为输入,这显然是编译时未知的。当然,我可以在运行时在代码中解析对象的类型,但为了使 API 正常工作,我还必须加载包含插件的程序集,这一步显然不会在典型的 MEF 应用程序中执行。我可以在运行时通过扫描新插件将被转储的目录并加载这些 dll-s 来做到这一点,但这不再感觉非常 MEFy ..还有其他方法吗?谢谢。
I would like to build an extensible NET application using MEF. This works very well for discovering and injecting instances of new types in my application. The problem I have is that some API I am using does not permit injecting new objects, but instead receives as input the type for those plugged in objects which is obviously unknown compile time. Of course I can resolve the type of an object at run time in my code but in order for the API to work I would also have to load the assembly containing the plugins, a step which is obviously not performed in a typical MEF application. I could do that at runtime by scanning the directory where new plugins will be dumped and load those dll-s but this does not feel very MEFy anymore .. Is there some other way? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,您使用 MEF 来组合对象,而不是查找类型。
但 MEF 确实实现了您在 DirectoryCatalog< 中描述的内容/a>.您可以使用
DirectoryCatalog.Parts
属性枚举使用 MEF 属性导出的部件。Normally you use MEF to compose objects, not to find types.
But MEF does implement what you describe in the DirectoryCatalog. You can enumerate parts exported with MEF attributes with the
DirectoryCatalog.Parts
property.