繁体   English   中英

在ClassLibrary中使用MEF

[英]using MEF in a ClassLibrary

我想在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”的“插件”属性。

任何人都有解决方案吗?

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

另外,你构成了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入门

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM