简体   繁体   English

在ClassLibrary中使用MEF

[英]using MEF in a ClassLibrary

I would like to use MEF in a ClassLibrary and not in an application project (neither ConsoleApplication nor WebApplication...). 我想在ClassLibrary中使用MEF,而不是在应用程序项目中使用(无论是ConsoleApplication还是WebApplication ......)。

When I try do this (like MSDN: http://msdn.microsoft.com/en-us/library/dd460648.aspx ) 当我尝试这样做时(如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
    }
}

The Property : 财产:

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

works fine. 工作正常。

EDIT: 编辑:

However, I would like to move the import property & the aggregate catalog creation in a ClassLibrary and not in my ConsoleApplication. 但是,我想将导入属性和聚合目录创建移动到ClassLibrary中而不是我的ConsoleApplication中。

Something like below. 像下面的东西。

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

And in the ConsoleApplication: 在ConsoleApplication中:

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

But when I do this change, I'm not able to get "Plugins" property which is always "null" in my ClassLibrary. 但是当我做这个改变时,我无法获得在我的ClassLibrary中始终为“null”的“插件”属性。

Anyone have a solution to do this ? 任何人都有解决方案吗?

This is probably just a problem with your catalog. 这可能只是您的目录的问题。 If you are moving your property into a class library, that implies a separate assembly. 如果要将属性移动到类库中,则意味着单独的程序集。 Currently, only the Assembly hosting "Program" is in your catalog, along with whatever is in "Extensions". 目前,只有托管“程序”的程序集在您的目录中,以及“扩展程序”中的任何内容。 So, did you make sure that your new class library is in the extensions folder? 那么,你确定你的新类库在扩展文件夹中吗?

Also, you are composing this meaning you are composing an instance of "Program" If you moved your property to a different class in a different library, then you need to compose an instance of whatever class that is. 另外,你构成了this意思,你正在编写一个“程序”实例如果你将你的属性移动到另一个库中的另一个类,那么你需要编写一个任何类的实例。

Something like 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
    }
}

If that doesn't work then try looking at your parts in Visual MEFX. 如果这不起作用,那么尝试在Visual MEFX中查看您的部件。 Here is a short guide to setting it up: Getting Started with Visual MEFX 以下是设置它的简短指南: Visual MEFX入门

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

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