What exactly is a “Special Class”?

后端 未结 7 1918
鱼传尺愫
鱼传尺愫 2020-12-23 13:21

After failing to get something like the following to compile:

public class Gen where T : System.Array
{
}

with the error

7条回答
  •  醉梦人生
    2020-12-23 13:21

    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;
    }
    

    Source: Binder_Constraints.cs IsValidConstraintType
    I've found it using a GitHub search: "A constraint cannot be special class"

提交回复
热议问题