0

MEF をアプリケーション プロジェクトではなく ClassLibrary で使用したいと考えています (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;

正常に動作します。

編集:

ただし、インポート プロパティと集約カタログの作成を ConsoleApplication ではなく ClassLibrary に移動したいと考えています。

以下のようなもの。

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

これはおそらくカタログの問題です。プロパティをクラスライブラリに移動する場合、それは別のアセンブリを意味します。現在、「プログラム」をホストしているアセンブリのみが、「拡張機能」にあるものとともにカタログにあります。では、新しいクラスライブラリがextensionsフォルダーにあることを確認しましたか?

また、作成している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
    }
}

それでもうまくいかない場合は、VisualMEFXでパーツを確認してみてください。設定の簡単なガイドは次のとおりです。VisualMEFX入門

于 2012-06-05T21:43:52.393 に答える