所以,问题是:
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 ?
所以我可以获得两种实现方式吗?
答案 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);
}