在查看此类代码时,我看不到其他解释:
public interface IParam
{
}
public class Param : IParam
{
}
public interface ITest<out T> where T : IParam
{
}
public class Test : ITest<Param>
{
}
public class Tester
{
public void Run<O>(ITest<O> a)
where O : IParam
{
Get(a); // ERROR
ITest<Param> b = new Test();
Get(b); // OK
}
public void Get(ITest<IParam> input)
{
}
}
尽管a
中的两种类型b
都是通过IParam
派生的类型参数有效地声明的,但这仅是第二遍编译。
到目前为止,我还没有找到通知说在处理协方差/协方差时会忽略约束。我是否缺少某些东西,或者确实是这种情况(忽略了约束)?