MEF InheritedExport 隐藏?
对于模糊的问题框架表示歉意。
情况是这样的。我在 Assembly1(类库)中有 ParentA。 ParentA 导出 typeof ParentA
现在,另一个产品团队想要在 ChildA 中覆盖 ParentA 的某些行为并部署其程序集 - Assembly2,其中包含对 Assembly1 的引用(显然)。
要求是 ParentA 被 ChildA 完全隐藏,并且导入 ParentA 的所有容器现在应该获得对 ChildA 实例的引用。标准继承的东西。
但是 - MEF 会导出 ParentA 和 ChildA 的实例吗?
我该如何解决这种情况?
Apologies for the vague question framing.
Here is the situation. I have ParentA in Assembly1( class library). ParentA exports typeof ParentA
Now another product team wants to override some of the behaviour of ParentA, in ChildA and deploy their Assembly - Assembly2, which holds a reference to Assembly1 (obviously).
The requriement is that ParentA is completely hidden by ChildA and all containers that import ParentA should now get a reference to an instance of ChildA instead. Standard Inheritance stuff.
BUT - Would MEF export instances of both ParentA AND ChildA?
How would i work around this situation ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这就是您正在寻找的内容:
http://msdn。 microsoft.com/en-us/library/ee155691.aspx#avoiding_discovery
它是这样说的:
Perhaps this is what you are / were looking for:
http://msdn.microsoft.com/en-us/library/ee155691.aspx#avoiding_discovery
This is what it says:
当 MEF 发现
ClassA
有两个导出时,它只需要一个,它会抛出一个CompositionException
,表示存在基数问题。它不知道如何在两者之间进行选择。有一种方法可以解决这个问题:如果将多个导出提供程序传递给容器,则容器在查找导出时将依次查询每个导出提供程序。第一个提供该零件的出口供应商获胜。
在以下示例中,“自定义”子文件夹中的程序集提供的导出将覆盖可执行文件文件夹中的程序集提供的导出。
编辑:
由于所描述的解决方案并不令人满意,我只能假设您使用的是
ImportMany
而不是Import
。在这种情况下,您确实仍然会获得两个导出,并且您必须添加一些 元数据。然后,您可以在导入类中编写代码来决定哪个导入是“最佳”。另请参阅此博文 作者:丹尼尔·普莱斯特。When MEF finds two exports for
ClassA
when it only expects one, it will throw aCompositionException
saying that there is a cardinality problem. It doesn't know how to choose between both.There is a way around this: if you pass multiple export providers to a container, the container will query each export provider in turn when it looks for an export. The first export provider to provide the part, wins.
In the following example the exports provided by assemblies in the "customized" subfolder override the exports provided by assemblies in the executable's folder.
edit:
Since the described solution isn't satisfactory, I can only assume that you are using
ImportMany
rather thanImport
. In that case you would indeed still get both exports, and you will have to add some metadata on them. You can then write code in your importing class which decides which import is the "best". See also this blog post by Daniel Plaisted.