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>>();有没有办法设置Windsor容器分辨率,以便bars同时包含FooImplA和FooImplB
发布于 2009-12-07 15:46:30
你不能。为什么?试着运行这个,这就是你想让Windsor做的。
var a = new FooImplA();
var b = new FooImplB();
var bars = new IFoo<BarImpl>[] { a, b };它不会编译。
发布于 2009-12-08 14:31:59
这就是我如何“解决”这个问题的.尽管我仍然在想,可能是我不理解我自己的问题,或者我在尝试做一些愚蠢的事情。
private static IEnumerable<object> ResolveTypeHierarchy(Type type, Type msgType) {
var handlers = new List<object>();
foreach (var interfaceType in msgType.GetInterfaces()) {
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;
}
return handlers;
}
ResolveTypeHierarchy(typeof(IFoo<>), typeof(BarImpl));
=> { FooImplA, FooImplB }我可能应该注意到,这是通过对Rhino.ServiceBus代码的研究和研究得出的。
https://stackoverflow.com/questions/1857596
复制相似问题