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