1

すべてのアセンブリをスキャンして、特定の属性を持つクラス(または抽象クラス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は、説明されているシナリオを処理するのに役立ちますか?はいの場合、どのように?

4

1 に答える 1

1

解決策はとても簡単でした!

私は2行を交換する必要がありました:

.FromAssembliesMatching("*") -> .FromAssembliesMatching(".") //mistake

.BindAllInterfaces() -> .BindBase() // because I'm implementing 
                                    // abstract class, not interface

属性は含まれていませんでした

于 2013-02-17T22:28:56.530 に答える