MEF 编程指南(十二):批量组合

本文介绍了MEF容器中批量组合的功能,通过批量添加或移除部件,容器能自动更新组合,简化了运行时动态调整部件的过程。文章还提供了具体的代码示例。

MEF 容器实例并非不可变的。如果目录支持改变(像监控目录变动)或者在运行时添加/移除部件都可能发生改变。以前,你不得不做出改动并且调用 CompositionContainer 上的 Compose 方法。在 Preview 4 release上,我们已经介绍对批量组合的支持。

 
批处理包含一系列添加或移除的部件。在执行更改之后,容器自动地触发一次更新重组导入导致的变化的组合。
设想一个设置窗口的场景,用户选中或者取消选中一个选项。那些会映射当前的部件或者不在容器中的部件。请求批处理,你将会调用 Compose 方法,如下: 
 
var batch = new CompositionBatch();
batch.AddPart(partInstance1);
batch.AddPart(partInstance2);
batch.RemovePart(part3);
 
container.Compose(batch);

 

对于类型实际上使用的特性编程模型,AttributedModel|Services 上有一些扩展方法用于 CompositionContainer 允许你在一些常规情况下隐藏 CompositionBatch。 
 
// creates a CompositionBatch and calls AddPart on all the passed parts followed by Compose
container.ComposeParts(partInstance1, partInstance2,... );
// creates a CompositionBatch and calls AddExportedValue<T> followed by Compose.
container.ComposeExportedValue<IFoo>(instanceOfIFoo); 

 

转载于:https://www.cnblogs.com/JavCof/p/3706869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值