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」プロパティを取得できません。
誰でもこれを行うための解決策を持っていますか?