Dependency injection Windsor解析通用服务子类型 接口IFoo{} 接口IBar{} 类BarImpl:IBar{} 类FooImplA:IFoo{} 类FooImplB:IFoo{} 集装箱。登记( 所有类型的(类型的(IFoo))。来自(assem) .WithService.FirstInterface()); var bar=container.ResolveAll();
是否需要设置Windsor容器分辨率,以便Dependency injection Windsor解析通用服务子类型 接口IFoo{} 接口IBar{} 类BarImpl:IBar{} 类FooImplA:IFoo{} 类FooImplB:IFoo{} 集装箱。登记( 所有类型的(类型的(IFoo))。来自(assem) .WithService.FirstInterface()); var bar=container.ResolveAll();,dependency-injection,castle-windsor,Dependency Injection,Castle Windsor,是否需要设置Windsor容器分辨率,以便条形图将同时包含FooImplA和FooImplB?您不能。为什么?试着运行这个,这就是你想要温莎做的 interface IFoo<T> { } interface IBar { } class BarImpl : IBar { } class FooImplA : IFoo<IBar> { } class FooImplB : IFoo<BarImpl> { } container.Register(
条形图
将同时包含FooImplA
和FooImplB
?您不能。为什么?试着运行这个,这就是你想要温莎做的
interface IFoo<T> { }
interface IBar { }
class BarImpl : IBar { }
class FooImplA : IFoo<IBar> { }
class FooImplB : IFoo<BarImpl> { }
container.Register(
AllTypes.Of(typeof(IFoo<>)).From(assem)
.WithService.FirstInterface());
var bars = container.ResolveAll<IFoo<BarImpl>>();
var a=new FooImplA();
var b=新的FooImplB();
变量条=新的IFoo[]{a,b};
它无法编译。您不能。为什么?试着运行这个,这就是你想要温莎做的
interface IFoo<T> { }
interface IBar { }
class BarImpl : IBar { }
class FooImplA : IFoo<IBar> { }
class FooImplB : IFoo<BarImpl> { }
container.Register(
AllTypes.Of(typeof(IFoo<>)).From(assem)
.WithService.FirstInterface());
var bars = container.ResolveAll<IFoo<BarImpl>>();
var a=new FooImplA();
var b=新的FooImplB();
变量条=新的IFoo[]{a,b};
它不会编译。好吧,这就是我“解决”这个问题的方法。。。虽然我仍然在想,可能是我不理解自己的问题,或者是在尝试一些愚蠢的事情
var a = new FooImplA();
var b = new FooImplB();
var bars = new IFoo<BarImpl>[] { a, b };
私有静态IEnumerable ResolveTypeHierarchy(类型,类型msgType){
var handlers=newlist();
foreach(msgType.GetInterfaces()中的var interfaceType){
var gType=type.MakeGenericType(interfaceType);
handlers.AddRange(container.ResolveAll(gType));
}
var baseType=msgType;
while(null!=baseType){
var gType=type.MakeGenericType(baseType);
handlers.AddRange(container.ResolveAll(gType));
baseType=baseType.baseType;
}
返回处理程序;
}
ResolveTypeHierarchy(typeof(IFoo)、typeof(BarImpl));
=>{FooImplA,FooImplB}
我可能应该注意到,这是从Rhino.ServiceBus代码的研究和窥视中获得的。好吧,这就是我“解决”这个问题的方式。。。虽然我仍然在想,可能是我不理解自己的问题,或者是在尝试一些愚蠢的事情
var a = new FooImplA();
var b = new FooImplB();
var bars = new IFoo<BarImpl>[] { a, b };
私有静态IEnumerable ResolveTypeHierarchy(类型,类型msgType){
var handlers=newlist();
foreach(msgType.GetInterfaces()中的var interfaceType){
var gType=type.MakeGenericType(interfaceType);
handlers.AddRange(container.ResolveAll(gType));
}
var baseType=msgType;
while(null!=baseType){
var gType=type.MakeGenericType(baseType);
handlers.AddRange(container.ResolveAll(gType));
baseType=baseType.baseType;
}
返回处理程序;
}
ResolveTypeHierarchy(typeof(IFoo)、typeof(BarImpl));
=>{FooImplA,FooImplB}
我可能应该注意到,这是从Rhino.ServiceBus代码的研究和窥视中获得的。是的。。。是的,我忘了提到BarImpl:IBar。所以castle不可能做到这一点?我真正想要的是IFoo作为返回类型,w/a特定impl作为查找类型。我意识到所有类型的IBar都必须注册。我无能为力?获取IFoo,找到容器中的所有类型,其中typeof(T).IsAssignableFrom(typeof(BarImpl))。是。。。是的,我忘了提到BarImpl:IBar。所以castle不可能做到这一点?我真正想要的是IFoo作为返回类型,w/a特定impl作为查找类型。我意识到所有类型的IBar都必须注册。我无能为力?获取IFoo,找到容器中的所有类型,其中typeof(T).IsAssignableFrom(typeof(BarImpl))。