2014-10-30 36 views
1

下面的代码将引发InvalidOperationException,消息:全球集装箱已初始化为什么枚举MEF部件会导致容器初始化失败?

 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()行,它工作正常。这里发生了什么?

回答

0

我从来没有想到它的细节,但大会目录做了很多延迟加载。调用ToArray()强制它完成延迟处理。