public abstract class EntityBase { ... }
public interface IFoobar
{
void Foo<T>(int x)
where T : EntityBase, new();
}
public interface IFoobar<T>
where T : EntityBase, new()
{
void Foo(int x);
}
public class Foobar<T> : IFoobar, IFoobar<T>
where T : EntityBase, new()
{
public void Foo(int x) { ... }
void IFoobar.Foo<T>(int x) { Foo(x); }
}
我收到编译器警告:Type parameter 'T' has the same name as the type parameter from outer type '...'
我试过做:void IFoobar.Foo<U>(int x) { Foo(x); }
,但是我不能保证 U 和 T 是一样的。Foobar 类的实现方式,它们必须相同,这一点非常重要。
我也尝试过这样做:void IFoobar.Foo<U>(int x) where U : T { Foo(x); }
,但这并不能保证 U 和 T 相等,并且它不允许我重新定义约束,因为它是在接口上定义的。