MEF を使用して、複数のアセンブリからエクスポートされた型を構成しています。ImportMany
派生クラスで指定されているように、依存関係がある基本クラスを使用しています。次のようになります。
ベース アセンブリ:
public abstract class BaseClass
{
[ImportMany(typeof(IDependency)]
public IEnumerable<IDependency> Dependencies { get; private set; }
protected BaseClass()
{
var catalog = GetCatalog();
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
protected abstract ComposablePartCatalog GetCatalog();
}
アセンブリ A:
[Export(typeof(BaseClass))]
public class MyA : BaseClass
{
protected override ComposablePartCatalog GetCatalog()
{
return new AssemblyCatalog(Assembly.GetExecutingAssembly());
}
}
[Export(typeof(IDependency)]
public class DependencyA1 : IDependency {}
[Export(typeof(IDependency)]
public class DependencyA2 : IDependency {}
アセンブリ B:
[Export(typeof(BaseClass))]
public class MyB : BaseClass
{
protected override ComposablePartCatalog GetCatalog()
{
return new AssemblyCatalog(Assembly.GetExecutingAssembly());
}
}
[Export(typeof(IDependency)]
public class DependencyB1 : IDependency {}
[Export(typeof(IDependency)]
public class DependencyB2 : IDependency {}
次に、ベース アセンブリですべてを構成します。
static void Main(string[] args)
{
DirectoryCatalog catalog = new DirectoryCatalog(path, "*.dll");
var container = new CompositionContainer(catalog);
IEnumerable<BaseClass> values = container.GetExportedValues<BaseClass>();
// both BaseClass instances now have 4 Dependencies - from both Assemby A and Assembly B!
}
MyA
MEF を使用して と の両方を構成するとMyB
、両方のアセンブリからエクスポートされIDependency
た -ies がそれぞれに含まれるという問題が発生します。と同じMyA
ように、エクスポートDependencyA1
とのみを含めたいと思います。DependencyA2
MyB
これにはおそらく依存性注入コンテナーを使用する必要があることはわかっていますが、MEF で実行できることを望んでいましたか?