views:

87

answers:

2
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>>();

Is there anyway to setup the Windsor container resolution so that bars will include both FooImplA and FooImplB?

+1  A: 

You can't. Why? Try to run this, which is what you want Windsor to do.

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

It won't compile.

Krzysztof Koźmic
yes... that's true, I forgot to mention that BarImpl : IBar. so there is no way that castle can do this? what I really want is is IFoo<IBar> as the return type, w/ a specific impl as the lookup type. i realize that all types of IBar would have to be registered. there is nothing custom i can do? get IFoo<T>, find all types in container where typeof(T).IsAssignableFrom(typeof(BarImpl)).
neouser99
A: 

Well, this is how I "solved" this... although I'm still thinking that it's possible either I didn't understand my own problem, or am attempting something that is silly.

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 }

I should probably note that this was taken from researching and peering through the Rhino.ServiceBus code.

neouser99