【发布时间】:2010-10-11 10:59:39
【问题描述】:
我想实现一个通用的 C# 类,大致如下:
abstract class Foobar<T> : AbstractBase, T
{ ... }
这失败了,因为 C# 将只允许基类之后的类型作为接口,所以接下来我试试这个:
abstract class Foobar<T> : AbstractBase, T where T : interface
{ ... }
但后来我发现 C# 不允许这种形式的类型约束。仅允许使用 where T : struct 和 where T : class。
如何规定类型参数只能是接口类型?
【问题讨论】:
-
你想达到什么目的?我真的想不出这种约束是必要的情况。
标签: c# generics type-constraints