0

我想在 ClassLibrary 中使用 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;

工作正常。

编辑:

但是,我想在 ClassLibrary 中而不是在我的 ConsoleApplication 中移动导入属性和聚合目录创建。

像下面的东西。

在 ClassLibrary [Manager.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());
        }
    }
}

在 ConsoleApplication 中:

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

但是,当我进行此更改时,我无法在我的 ClassLibrary 中获得始终为“null”的“Plugins”属性。

有人有解决方案吗?

4

1 回答 1

2

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

此外,您正在编写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 入门

于 2012-06-05T21:43:52.393 回答