仅当某些外部库满足时MEF导出/导入
有没有办法告诉 MEF 仅在满足某些其他依赖项 dll/库的情况下才尝试加载 dll?我正在尝试编写一个依赖于另一个扩展的扩展。我希望能够检查其他扩展是否存在,而不是仅尝试加载我的扩展。有可能吗?我已经找到了如何使用以下代码(在 StackOverflow 上的某处找到)检查某些 dll 是否存在:
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr LoadLibrary(string fileName);
static bool CheckLibrary(string fileName)
{
return LoadLibrary(fileName) != IntPtr.Zero;
}
现在我希望能够仅加载那些满足依赖关系的 dll/扩展。
Is there a way to tell MEF to try to load a dll only if some other dependency dlls/ libraries are satisfied? I'm trying to write an extension that is dependent on another extension. I want to be able to check whether other extension exists or not than only try to load my extension. Is it even possible? I've already found out how to check whether certain dll exists or not by using following code (found somewhere on StackOverflow):
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr LoadLibrary(string fileName);
static bool CheckLibrary(string fileName)
{
return LoadLibrary(fileName) != IntPtr.Zero;
}
Now I like to be able to load only those dlls/ extensions who have their dependency satisfied.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MEF 通过拒绝自然地做到了这一点。假设您希望仅当扩展 B 存在时才加载扩展 A。据推测,这是因为扩展 A 依赖于扩展 B 提供的某些功能。在 MEF 中表达这一点的方法是通过导入(基数 ExactlyOne,即不是集合导入且不是可选的)。
因此,扩展 A 应该具有所需的导入,该导入可以通过扩展 B 的导出来满足。如果扩展 B 不存在,则导入将无法得到满足,并且扩展 A 将被拒绝,导致其在以下位置不可用容器。
我在我的博客文章中对此进行了更多解释:如何调试和诊断 MEF 故障。
MEF does this naturally through rejection. Say you want extension A to load only if extension B is present. Presumably this is because extension A depends on some functionality which extension B provides. The way to express this in MEF is via an import (of cardinality ExactlyOne, ie not a collection import and not optional).
So extension A should have a required import which is satisfied by an export from extension B. If extension B is not present, then the import will not be able to be satisfied, and extension A will be rejected, causing it to not be available in the container.
I explain a little more about this in my blog post: How to Debug and Diagnose MEF Failures.