C# 在类库中使用MEF

C# 在类库中使用MEF,c#,mef,C#,Mef,我希望在类库中使用MEF,而不是在应用程序项目中使用(无论是ConsoleApplication还是WebApplication…) 当我尝试这样做时(如MSDN:) 财产: [Import(typeof(IPlugin))] public IPlugin Plugins; 很好 编辑: 但是,我希望在类库中而不是在控制台应用程序中移动导入属性&聚合目录创建 像下面这样 在类库[Manager.dll]中: public class Manager { private

我希望在类库中使用MEF,而不是在应用程序项目中使用(无论是ConsoleApplication还是WebApplication…)

当我尝试这样做时(如MSDN:)

财产:

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

编辑:

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

像下面这样

在类库[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());
        }
    }
}
在控制台应用程序中:

class Manager
{
    static void Main(string[] args)
    {
        Manager m = new Manager(); //Composition is performed in the constructor
    }
}
但是当我做这个更改时,我无法获得“Plugins”属性,它在我的类库中总是“null”


有人有解决方法吗?

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

此外,您正在编写
意味着您正在编写“程序”的实例。如果您将属性移动到不同库中的不同类,那么您需要编写任何类的实例

大概是这样的:

    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中查看您的零件。下面是一个简短的设置指南:

简单地将所有插件放在类库中,并向程序添加引用和名称空间声明,有什么问题吗?我的印象是MEF管道本身(包括导入)需要在主应用程序中才能正常工作。请参见“”。@RobertHarvey:我不想将所有插件放在同一类库中,我也不能。我同意你的感受。。。我认为导入需要在主应用程序中才能正常工作,但我认为这不是一个好的想法,因为它可能是重复代码的来源。
    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
    }
}