到底是什么"特级"?
未能得到类似后,以下编译:
After failing to get something like the following to compile:
public class Gen<T> where T : System.Array
{
}
与错误
with the error
一个约束不能特殊类'的System.Array
A constraint cannot be special class `System.Array'
我开始想,究竟是什么的是的一个特殊类?
I started wondering, what exactly is a "special class"?
人们似乎常常得到同样的错误时他们在一个通用的限制指定 System.Enum
。我买了 System.Object的
, System.Delegate
, System.MulticastDelegate 和
System.ValueType
了。
People often seem to get the same kind of error when they specify System.Enum
in a generic constraint. I got the same results with System.Object
, System.Delegate
, System.MulticastDelegate
and System.ValueType
too.
是否有更多的人?我找不到在C#中的特殊班的任何信息。
Are there more of them? I cannot find any info on "special classes" in C#.
此外,什么的是的特别之处那些我们不能使用类?他们作为一个泛型类型约束
Also, what is so special about those classes that we can't use them as a generic type constraint?
从罗斯林源代码,它看起来像硬编码类型的列表:
From the Roslyn source code, it looks like a list of hardcoded types:
switch (type.SpecialType)
{
case SpecialType.System_Object:
case SpecialType.System_ValueType:
case SpecialType.System_Enum:
case SpecialType.System_Delegate:
case SpecialType.System_MulticastDelegate:
case SpecialType.System_Array:
// "Constraint cannot be special class '{0}'"
Error(diagnostics, ErrorCode.ERR_SpecialTypeAsBound, syntax, type);
return false;
}
来源:的 Binder_Constraints.cs IsValidConstraintType 结果
我发现它使用的是GitHub上的搜索:的约束不能特殊类