2009-12-07 3 views
0
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>>(); 

는 설치 윈저 컨테이너 해상도 어쨌든이 있나요?윈저 해결 일반 서비스 아형

답변

3

수 없습니다. 왜? 이것을 실행하면 윈저가 원하는 결과를 얻으실 수 있습니다.

var a = new FooImplA(); 
var b = new FooImplB(); 
var bars = new IFoo<BarImpl>[] { a, b }; 

컴파일되지 않습니다.

+0

예 ... 그렇습니다. 저는 BarImpl : IBar에 대해 언급하는 것을 잊었습니다. 그래서 성에서 할 수있는 방법이 없습니까? 내가 진정으로 원하는 것은 IFoo 을 반환 유형으로, 특정 impl을 조회 유형으로 사용하는 것입니다. 나는 모든 유형의 IBar를 등록해야한다는 것을 알고 있습니다. 내가 할 수있는 관례가 없니? IFoo 을 얻으려면 container에 typeof (T) .IsAssignableFrom (typeof (BarImpl))이있는 모든 유형을 찾으십시오. – neouser99

0

글쎄,이게 내가 이것을 "해결"한 것입니다. 나는 아직도 내 자신의 문제를 이해하지 못했거나 바보 같은 것을 시도 할 수 있다고 생각합니다.

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 코드를 조사하고 들여다 본 결과입니다.