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); }
}
我收到了一个编译器警告:
类型参数'T'与外部类型的类型参数相同...
我尝试过这样做:void IFoobar.Foo<U>(int x) { Foo(x); }
,但是这样做无法保证U和T相同。由于Foobar类的实现方式非常重要,所以它们必须相同。我还尝试过这样做:
void IFoobar.Foo<U>(int x) where U : T { Foo(x); }
,但是这并不能保证U和T相等,也不能重新定义约束条件,因为它已经在接口中定义了。