为什么枚举MEF部分会导致容器初始化失败
本文关键字:初始化 失败 枚举 MEF 为什么 | 更新日期: 2023-09-27 18:28:33
以下代码引发InvalidOperationException,消息:全局容器已初始化
var aggcat = new AggregateCatalog();
aggcat.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly().Location));
// This line does something to the catalog
aggcat.Parts.ToArray();
_container = new CompositionContainer(aggcat, true);
// Exception gets thrown here
System.ComponentModel.Composition.Hosting.CompositionHost.Initialize(_container);
当我注释掉Parts.ToArray()行时,它工作得很好。这里发生了什么?
我从来没有弄清楚它的细节,但Assembly Catalog做了很多懒惰的加载。调用ToArray()将强制它完成延迟处理。