すべてのアセンブリをスキャンして、特定の属性を持つクラス(または抽象クラスColorTestから継承されたクラス)を探し、それらを自動的にColorTestにバインドする必要があります。次に、 ColorTestのすべての実装をインスタンス化して列挙する必要があります
アセンブリ1:
public abstract class ColorTest {
public abstract string GetColorString();
}
アセンブリ2:
//[ColorImplementation] // attributes are not necessary
public class BlueColor() : ColorTest {...}
アセンブリ3:
//[ColorImplementation] //not necessary
public class RedColor() : ColorTest {...}
アセンブリ4:
class Program{
public static Main(){
#region Problem area :)
var kernel = new StandardKernel();
kernel.Bind(x => x
.FromAssembliesMatching("*")
.SelectAllClasses()
.InheritedFrom<ColorTest>// or .WithAttribute<ObsoleteAttribute>()
.BindAllInterfaces() // I'd like to Bind it only to ColorTest
.Configure(b => b.InSingletonScope()));
#endregion
// Enumerate
kernel
.GetAll<ColorTest>()
.ToList()
.ForEach(t=>Console.WriteLine(t.GetColorString()));
}
例は、より複雑な問題の抽象化と単純化です。
Ninjectは、説明されているシナリオを処理するのに役立ちますか?はいの場合、どのように?