我需要扫描所有程序集以查找具有特定属性的类(或从抽象类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 可以帮我处理描述的场景吗?如果是怎么办?