在类库中使用MEF

时间:2013-02-08 21:39:37

标签: mef

好的,所以在类库中,不使用MEF是个好主意吗?

以下是一个例子:

ISomeInterface

5 ISomeInterface的实现

导入所有ISomeInterface并使用它们的类。

同样,这都在一个单独的dll中。因为它在DLL中,所以没有MEF的引导来创建目录,并且构建目录只是为了使用它一次似乎有点多。

我只是在学习MEF以及如何使用它。

格雷格

1 个答案:

答案 0 :(得分:1)

在阅读了更多内容之后,看起来没有理由不能在DLL内部使用MEF来创建自己的部件。当我问这个问题时,我认为导入主要在Main()或App()类型的函数内构成整个app。但是如果需要在导出到应用程序的主要部分上进行编写,它仍然可以使用MEF在构造函数中组合自己,如下所示:

//An aggregate catalog that combines multiple catalogs
        var catalog = new AggregateCatalog();
        //Adds all the parts found in the same assembly as the Program class
        catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));

        //Create the CompositionContainer with the parts in the catalog
        _container = new CompositionContainer(catalog);

        //Fill the imports of this object
        try
        {
            this._container.ComposeParts(this);
        }
        catch (CompositionException compositionException)
        {

        }