使用CompositionBatch对象从CompositionContainer移除零件 [英] Removing parts from CompositionContainer using a CompositionBatch object

查看:267
本文介绍了使用CompositionBatch对象从CompositionContainer移除零件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于MEF的解决方案,其中包含接口的多个导出实现.

I have a MEF based solution that has several exported implementations of an interface.

我想做的是有一个开关,该开关删除 ALL 与该接口关联的当前部分,并用一个单独的实现代替它们.我一直在尝试使用CompositionBatch对象执行此操作,但是它似乎不起作用.这是我正在做的事的一个例子:

What I want to be able to do is have a switch that removes ALL the current parts associated with the interface, and in their place, replace them with just a single implementation. I've been trying to do this with a CompositionBatch object, but it doesn't seem to work. Here's an example of what I'm doing:

[Export(typeof(IFoo)]
public class Foo1 : IFoo
{ }

[Export(typeof(IFoo)]
public class Foo2 : IFoo
{ }

然后我有我的容器:

var container = new CompositionContainer(....);

现在将包含代表Foo1Foo2的部分.我想做的是将它们替换为另一个IFoo实现.这是我正在尝试的方法,我认为这可以工作:

which will now contain parts representing Foo1 and Foo2. What I want to do, is replace them with another IFoo implementation. This is what I'm trying, and I thought that this would work:

var partsToRemove
   = from part in container.Catalog.Parts
       from exDef in part.ExportDefinitions
       where exDef.ContractName == AttributedModelServices.GetContractName(typeof(IFoo))
     select part.CreatePart();

var batch = new CompositionBatch(null, partsToRemove);

batch.AddPart(new Foo3());

container.Compose(batch);

我希望container.Catalog.Parts进行更改以反映我的更改,但事实并非如此.与第一次创建容器时相同.

I'm expecting container.Catalog.Parts to change in order to reflect my changes, but it doesn't. It remains the same as when the container was first created.

我想念什么?这是正确的方法吗?我已阅读 Glenn Block的CodeBetter文章关于使用ExportProviders ,但是他提到他将编写第2部分,其中将介绍实现过滤ExportProvider(可能更接近我需要做的事情).

What am I missing? Is this even the right approach? I've read Glenn Block's CodeBetter article on using ExportProviders, but he mentions that he'll write a part 2 in which he'll look at implementing a filtering ExportProvider (which may be closer to what I need to do).

推荐答案

因此,CompositionBatch与添加和删除显式对象实例有关,并且不与目录连接,而是与添加一组定义(如果可以的话,也称为类型)有关.然后将其构造到CatalogExportProvider中的对象实例中.要执行您想要的操作,您需要在将其传递到容器以排除所需类型之前实际过滤目录. (请参阅 http://mef.codeplex.com/wikipage?title=Filtering%20Catalogs作为过滤目录的示例.

So CompositionBatch is about adding and removing explicit object instances and is not connected to the catalog, which is about adding a set of definitions (aka types if you will) that are then later constructed into object instances in the CatalogExportProvider. To do what you want you will need to actually filter to catalog before you pass it to the container to exclude the types you want. (See http://mef.codeplex.com/wikipage?title=Filtering%20Catalogs for an example of a filtering catalog).

然后,如果要添加显式的Foo实例,可以为此使用CompositionBatch.

Then if you want to add an explicit Foo instance you can use a CompositionBatch for that.

这篇关于使用CompositionBatch对象从CompositionContainer移除零件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆