开发者

Is Castle Windsor able to resolve contravariant generics?

开发者 https://www.devze.com 2023-01-29 17:06 出处:网络
So, the problem: interface IControl<in T> where T : Base { void Execute(T item); } class ControlDerived : ICon开发者_如何学编程trol<Derived>

So, the problem:

interface IControl<in T> where T : Base
{
    void Execute(T item);
}


class ControlDerived : ICon开发者_如何学编程trol<Derived>
{
    public void Execute(Derived item)
    {
    }
}

class ControlBase : IControl<Base>
{
    public void Execute(Base item)
    {
    }
}

class Derived : Base
{
}

Is it possible to make Windsor work like this:

var ctls = container.ResolveAll<IControl<Derived>>()
ctls.Lenght == 2 ?

So I could get both implementations?


You're lazy :p

I ran the following test and it passes

[Test]
public void ResolveAll_can_resolve_contravariant_components()
{
 Container.Register(Component.For<IAmContravariant<Base>>().ImplementedBy<ContravariantBase>(),
              Component.For<IAmContravariant<Derived>>().ImplementedBy<ContravariantDerived>());

 var all= Container.ResolveAll<IAmContravariant<Derived>>();
 Assert.AreEqual(2, all.Length);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消