首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windsor解析通用服务SubTypes

Windsor解析通用服务SubTypes
EN

Stack Overflow用户
提问于 2009-12-07 11:08:40
回答 2查看 316关注 0票数 0
代码语言:javascript
复制
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同时包含FooImplAFooImplB

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-12-07 15:46:30

你不能。为什么?试着运行这个,这就是你想让Windsor做的。

代码语言:javascript
复制
var a = new FooImplA();
var b = new FooImplB();
var bars = new IFoo<BarImpl>[] { a, b };

它不会编译。

票数 3
EN

Stack Overflow用户

发布于 2009-12-08 14:31:59

这就是我如何“解决”这个问题的.尽管我仍然在想,可能是我不理解我自己的问题,或者我在尝试做一些愚蠢的事情。

代码语言:javascript
复制
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代码的研究和研究得出的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1857596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档