MEF InheritedExport 隐藏?

发布于 2024-10-14 18:30:49 字数 327 浏览 2 评论 0原文

对于模糊的问题框架表示歉意。

情况是这样的。我在 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 技术交流群。

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

发布评论

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

评论(2

回眸一遍 2024-10-21 18:30:49

也许这就是您正在寻找的内容:

http://msdn。 microsoft.com/en-us/library/ee155691.aspx#avoiding_discovery

它是这样说的:

在某些情况下,您可能希望防止某个部件被发现为
目录的一部分。例如,该部分可能是一个基类
可以继承,但不能使用。有两种方法可以实现
这。首先,您可以在零件类上使用abstract 关键字。
抽象类从不提供导出,尽管它们可以提供
继承导出到派生自它们的类。

如果类不能被抽象化,你可以用
PartNotDiscoverable 属性。用此属性装饰的部分
不会包含在任何目录中。下面的例子
演示了这些模式。 DataOne 将被
目录。由于DataTwo是抽象的,因此不会被发现。自从
DataThree 使用了 PartNotDiscoverable 属性,它不会
发现了。

<Export()>
Public Class DataOne
    'This part will be discovered 
    'as normal by the catalog.
End Class

<Export()>
Public MustInherit Class DataTwo
    'This part will not be discovered
    'by the catalog.
End Class

<PartNotDiscoverable()>
<Export()>
Public Class DataThree
    'This part will also not be discovered
    'by the catalog.
End Class

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:

In some cases, you may want to prevent a part from being discovered as
part of a catalog. For example, the part may be a base class intended
to be inherited from, but not used. There are two ways to accomplish
this. First, you can use the abstract keyword on the part class.
Abstract classes never provide exports, although they can provide
inherited exports to classes that derive from them.

If the class cannot be made abstract, you can decorate it with the
PartNotDiscoverable attribute. A part decorated with this attribute
will not be included in any catalogs. The following example
demonstrates these patterns. DataOne will be discovered by the
catalog. Since DataTwo is abstract, it will not be discovered. Since
DataThree used the PartNotDiscoverable attribute, it will not be
discovered.

<Export()>
Public Class DataOne
    'This part will be discovered 
    'as normal by the catalog.
End Class

<Export()>
Public MustInherit Class DataTwo
    'This part will not be discovered
    'by the catalog.
End Class

<PartNotDiscoverable()>
<Export()>
Public Class DataThree
    'This part will also not be discovered
    'by the catalog.
End Class
满栀 2024-10-21 18:30:49

当 MEF 发现 ClassA 有两个导出时,它只需要一个,它会抛出一个 CompositionException ,表示存在基数问题。它不知道如何在两者之间进行选择。

有一种方法可以解决这个问题:如果将多个导出提供程序传递给容器,则容器在查找导出时将依次查询每个导出提供程序。第一个提供该零件的出口供应商获胜。

在以下示例中,“自定义”子文件夹中的程序集提供的导出将覆盖可执行文件文件夹中的程序集提供的导出。

var defaultExportProvider = 
    new CatalogExportProvider(new DirectoryCatalog(".","*"));
var customizedExportProvider = 
    new CatalogExportProvider(new DirectoryCatalog(@".\customized"));
var container = new CompositionContainer(
    customizedExportProvider, defaultExportProvider);
defaultExportProvider.SourceProvider = container;
customizedExportProvider.SourceProvider = container;

编辑

由于所描述的解决方案并不令人满意,我只能假设您使用的是 ImportMany 而不是 Import。在这种情况下,您确实仍然会获得两个导出,并且您必须添加一些 元数据。然后,您可以在导入类中编写代码来决定哪个导入是“最佳”。另请参阅此博文 作者:丹尼尔·普莱斯特

When MEF finds two exports for ClassA when it only expects one, it will throw a CompositionException 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.

var defaultExportProvider = 
    new CatalogExportProvider(new DirectoryCatalog(".","*"));
var customizedExportProvider = 
    new CatalogExportProvider(new DirectoryCatalog(@".\customized"));
var container = new CompositionContainer(
    customizedExportProvider, defaultExportProvider);
defaultExportProvider.SourceProvider = container;
customizedExportProvider.SourceProvider = container;

edit:

Since the described solution isn't satisfactory, I can only assume that you are using ImportMany rather than Import. 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.

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