[英]Constraints on type parameters - where T: class
I have problem with constraints, because I want, that the type argument is a reference type just one of 3 classes, not the any others, so constraint "where L: class" is not ok. 我对约束有问题,因为我想要,类型实参是引用类型,只是3个类之一,而不是其他任何一个,因此约束“ where L:class”不可行。
Here is example: 这是示例:
public class MyClass <L>
where L : Circle
where L: Rectangle
where L: Triangle
This mean, that must comply with all constraints. 这意味着必须遵守所有约束条件。 Unfortunately, I have not found the answer yet.
不幸的是,我还没有找到答案。
Thank you very much. 非常感谢你。
You should create a base class or interface that Circle
, Rectangle
, and Triangle
inherit from. 您应该创建
Circle
, Rectangle
和Triangle
继承的基类或接口。
For example: 例如:
interface IShape
{
}
class Circle : IShape
{
// ...
}
class Rectangle : IShape
{
// ...
}
class Triangle : IShape
{
// ...
}
Then do the constraint on IShape
: 然后对
IShape
进行约束:
public class MyClass <L>
where L : IShape
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.