我有两个类,一个是基类,一个是子类。在基类中,我定义了一个通用的虚方法:
protected virtual ReturnType Create<T>() where T : ReturnType {}然后在我的子类中,我试图这样做:
protected override ReturnTypeChild Create<T>() // ReturnTypeChild 继承自 ReturnType { return base.Create<T> as ReturnTypeChild; }Visual Studio 给出了一个奇怪的错误:
The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Create()'. There is no boxing conversion or type parameter conversion from 'T' to 'ReturnType'.在子类的重写中重复使用 where 子句也会导致错误:
Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly那么我在这里做错了什么?
protected override BarChild Foo<T>()
吧?能不能用更合适的名字?Foo 和 Bar 让我很困惑。 - Kobi