标签:
[ImportMany(typeof(ILogger))] public IEnumerable<ILogger> Loggers { get; set; }上述代码和单个部件的导入有两点区别:
static void Main(string[] args) { Program program = new Program(); program.Compose(); foreach (ILogger logger in program.Loggers) { logger.WriteLog("Hello World!"); } Console.ReadLine(); }
/// <summary> /// 通过容器对象将宿主和部件组装到一起。 /// </summary> public void Compose() { AggregateCatalog aggregateCatalog = new AggregateCatalog(); AssemblyCatalog assemblyCatalog = new AssemblyCatalog(typeof(Program).Assembly); DirectoryCatalog directoryCatalog = new DirectoryCatalog("imps"); aggregateCatalog.Catalogs.Add(assemblyCatalog); aggregateCatalog.Catalogs.Add(directoryCatalog); var container = new CompositionContainer(aggregateCatalog); container.ComposeParts(this); }上述代码中有一个AggregateCatalog的目录类,这是一个聚合目录,它可以将多个目录组织在一起,比如上列中的AssemblyCatalog和DirectoryCatalog。这样,MEF就会在多个地方去搜寻匹配的导出部件。
[MEF]第04篇 MEF的多部件导入(ImportMany)和目录服务
标签:
原文地址:http://blog.csdn.net/gjysk/article/details/44948081