MEF:将类型与对象一起注入?

发布于 2024-12-03 07:37:43 字数 258 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小兔几 2024-12-10 07:37:43

我可以在运行时这样做
扫描新插件将被转储的目录并加载它们
dll-s 但这不再感觉很 MEFy ..还有其他的吗
方式?

通常,您使用 MEF 来组合对象,而不是查找类型。

但 MEF 确实实现了您在 DirectoryCatalog< 中描述的内容/a>.您可以使用 DirectoryCatalog.Parts 属性枚举使用 MEF 属性导出的部件。

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?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文