我需要扫描所有程序集以查找具有特定属性的类(或从抽象类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()));
}
示例是对更复杂问题的抽象和简化。
尼尼姆能帮我处理所描述的场景吗?如果是,怎么做?
发布于 2013-02-17 22:28:56
解决办法很简单!
我不得不换了两行:
.FromAssembliesMatching("*") -> .FromAssembliesMatching(".") //mistake
和
.BindAllInterfaces() -> .BindBase() // because I'm implementing
// abstract class, not interface
不涉及属性
https://stackoverflow.com/questions/14925740
复制相似问题