C# 为什么枚举MEF部件会导致容器初始化失败?

C# 为什么枚举MEF部件会导致容器初始化失败?,c#,mef,composition,C#,Mef,Composition,以下代码引发InvalidOperationException,消息:全局容器已初始化 var aggcat = new AggregateCatalog(); aggcat.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly().Location)); // This line does something to the catalog aggcat.Pa

以下代码引发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);

当我注释掉零件时,它工作得很好。这里发生了什么?

我从来没有弄清楚它的细节,但是程序集目录进行了大量的延迟加载。调用ToArray将强制它完成延迟处理