在类库中使用 MEF

本文关键字:MEF 类库 | 更新日期: 2023-09-27 18:36:17

我想在类库中使用MEF,而不是在应用程序项目中使用MEF(既不是ConsoleApplication也不是WebApplication...)。

当我尝试这样做时(如MSDN:http://msdn.microsoft.com/en-us/library/dd460648.aspx)

class Program
{
    private CompositionContainer _container;
    [Import(typeof(IPlugin))]
    public IPlugin Plugins;

    private Program()
    {
        //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));
        catalog.Catalogs.Add(new DirectoryCatalog(@".'Extensions"));

        //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)
        {
            Console.WriteLine(compositionException.ToString());
        }
    }

    static void Main(string[] args)
    {
        Program p = new Program(); //Composition is performed in the constructor
    }
}

该物业 :

    [Import(typeof(IPlugin))]
    public IPlugin Plugins;

工作正常。

编辑:

但是,我想在类库中而不是在我的控制台应用程序中移动导入属性和聚合目录创建。

如下所示。

在类库 [管理器.dll] 中:

public class Manager
{
    private CompositionContainer _container;
    [Import(typeof(IPlugin))]
    public IPlugin Plugins;

    public Manager()
    {
        //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(Manager).Assembly));
        catalog.Catalogs.Add(new DirectoryCatalog(@".'Extensions"));

        //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)
        {
            Console.WriteLine(compositionException.ToString());
        }
    }
}

在控制台应用程序中:

class Manager
{
    static void Main(string[] args)
    {
        Manager m = new Manager(); //Composition is performed in the constructor
    }
}

但是当我进行此更改时,我无法在我的类库中获得始终为"null"的"插件"属性。

有人有解决方案吗?

在类库中使用 MEF

这可能只是您的目录的问题。 如果要将属性移动到类库中,则意味着使用单独的程序集。 目前,目录中只有承载"程序"的程序集以及"扩展"中的任何内容。 那么,您是否确保新类库位于扩展文件夹中?

此外,您正在编写this这意味着您正在编写"程序"的实例如果将属性移动到不同库中的其他类,则需要编写任何类的实例。

像这样:

    class Program
   {
    private CompositionContainer _container;       
    private Program()
    {
        var helper = new MyOtherHelperClass();
        var catalog = helper.CreateCatalog();
        //Create the CompositionContainer with the parts in the catalog
        _container = new CompositionContainer(catalog);
        //Fill the imports of some object
        try
        {
            var thingIWantToCompose = new OtherClassWithAnImport();
            this._container.ComposeParts(thingIWantToCompose);
        }
        catch (CompositionException compositionException)
        {
            Console.WriteLine(compositionException.ToString());
        }
    }

    static void Main(string[] args)
    {
        Program p = new Program(); //Composition is performed in the constructor
    }
}

如果这不起作用,请尝试在Visual MEFX中查看您的部分。 下面是设置它的简短指南:Visual MEFX 入门