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

时间:2014-10-30 17:58:35

标签: c# mef composition

以下代码抛出InvalidOperationException消息:Global Container Already Initialized

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

1 个答案:

答案 0 :(得分:0)

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