Castle Windsor能够解决逆变仿制药吗?

时间:2010-12-10 14:09:26

标签: castle-windsor ioc-container

所以,问题是:

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


class ControlDerived : IControl<Derived>
{
    public void Execute(Derived item)
    {
    }
}

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

class Derived : Base
{
}

是否有可能让温莎像这样工作:

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

所以我可以获得两种实现方式吗?

1 个答案:

答案 0 :(得分:1)

你很懒:p

我运行了以下测试并通过了

[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);
}