[英]Inherited generic as a function parameter
How can I accomplish this? 我怎么能做到这一点?
class A { }
class B : A { }
class X<T> where T : A { }
class Y<T> : X<T> where T : A { }
private static void f(X<A> x) { }
public static void Main(string[] args)
{
Y<B> y = new Y<B>();
f(y); // Compiler error here
}
Y inherited from X, B from A, but it doesn't get compiled. Y从A继承自X,B,但它没有被编译。
Change the function definition to: 将函数定义更改为:
private static void f<T>(X<T> x) where T : A { }
As you have defined it, you are saying that f()
must be passed an instance of X<A>
. 正如您所定义的那样,您说
f()
必须传递X<A>
的实例。 By defining as I've shown here, you are saying that f()
takes any class that has X<A>
as a parent. 通过定义我在这里显示,你说
f()
接受任何具有X<A>
作为父类的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.