组合部件通过 [System.ComponentModel.Composition.ImportAttribute] 特性声明导入。类似于导出,也有几种不同的方法声明导入,即通过:字段(Fields)、属性(Properties)和构造器参数(Constructor Parameters)。
[Import] public IMessageSender MessageSender { get; set; }
[ImportingConstructor] public Program(IMessageSender messageSender) { }
[Import] private IMessageSender _messageSender;
public class Notifier { [ImportMany(AllowRecomposition=true)] public IEnumerable<IMessageSender> Senders { get; set; } public void Notify(string message) { foreach (IMessageSender sender in Senders) { sender.Send(message); } } }
public void OnImportsSatisfied() { // when this is called, all imports that could be satisfied have been satified. }
MEF 编程指南(四):声明导入,布布扣,bubuko.com
原文:http://www.cnblogs.com/JavCof/p/3673665.html