关于使用约束的泛型接口的继承问题
namespace NS
{
class A
{
public void F<TFrom, TTo>() where TTo : TFrom
{ }
}
interface IB
{
void F<TFrom, TTo>() where TTo : TFrom;
}
class B : IB
{
private readonly A _a = new A();
#region “显式实现”接口 IB 的成员
void IB.F<TFrom, TTo>()
{
_a.F<TFrom, TTo>();
//此处的错误信息为:The type 'TTo' must be convertible to 'TFrom' in order to use it as parameter 'TTo' in the generic method 'void NS.A.F<TFrom,TTo>()'。
//按我理解编译器应该知道此方法所实现的接口中已经规定了TTo被约束为从TFrom继承
//,但既然出现了此错误,说明编译器未智能到这地步,那么该怎么办?
//现在的要求是:在接口中一定要有约束,子类一定要“显式实现”接口。
}
#endregion
}
}