C# 构造一个System.Char,它将具有布尔System.TypeCode

C# 构造一个System.Char,它将具有布尔System.TypeCode,c#,enums,char,C#,Enums,Char,如何能够构造一个具有布尔值的System.TypeCode的System.Char 代码如下: char c = 'f'; System.TypeCode a = c.GetTypeCode(); Console.WriteLine(a); 生成以下输出: Char 现在我检查了系统的反编译版本。TypeCode。这是: #region Assembly System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f

如何能够构造一个具有布尔值的
System.TypeCode
System.Char

代码如下:

char c = 'f';
System.TypeCode a = c.GetTypeCode();
Console.WriteLine(a);
生成以下输出:

Char
现在我检查了
系统的反编译版本。TypeCode
。这是:

#region Assembly System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// System.Runtime.dll
#endregion

namespace System
{
    //
    // Summary:
    //     Specifies the type of an object.
    public enum TypeCode
    {
        //
        // Summary:
        //     A null reference.
        Empty = 0,
        //
        // Summary:
        //     A general type representing any reference or value type not explicitly represented
        //     by another TypeCode.
        Object = 1,
        //
        // Summary:
        //     A database null (column) value.
        DBNull = 2,
        //
        // Summary:
        //     A simple type representing Boolean values of true or false.
        Boolean = 3,
        //
        // Summary:
        //     An integral type representing unsigned 16-bit integers with values between 0
        //     and 65535. The set of possible values for the System.TypeCode.Char type corresponds
        //     to the Unicode character set.
        Char = 4,
        //
        // Summary:
        //     An integral type representing signed 8-bit integers with values between -128
        //     and 127.
        SByte = 5,
        //
        // Summary:
        //     An integral type representing unsigned 8-bit integers with values between 0 and
        //     255.
        Byte = 6,
        //
        // Summary:
        //     An integral type representing signed 16-bit integers with values between -32768
        //     and 32767.
        Int16 = 7,
        //
        // Summary:
        //     An integral type representing unsigned 16-bit integers with values between 0
        //     and 65535.
        UInt16 = 8,
        //
        // Summary:
        //     An integral type representing signed 32-bit integers with values between -2147483648
        //     and 2147483647.
        Int32 = 9,
        //
        // Summary:
        //     An integral type representing unsigned 32-bit integers with values between 0
        //     and 4294967295.
        UInt32 = 10,
        //
        // Summary:
        //     An integral type representing signed 64-bit integers with values between -9223372036854775808
        //     and 9223372036854775807.
        Int64 = 11,
        //
        // Summary:
        //     An integral type representing unsigned 64-bit integers with values between 0
        //     and 18446744073709551615.
        UInt64 = 12,
        //
        // Summary:
        //     A floating point type representing values ranging from approximately 1.5 x 10
        //     -45 to 3.4 x 10 38 with a precision of 7 digits.
        Single = 13,
        //
        // Summary:
        //     A floating point type representing values ranging from approximately 5.0 x 10
        //     -324 to 1.7 x 10 308 with a precision of 15-16 digits.
        Double = 14,
        //
        // Summary:
        //     A simple type representing values ranging from 1.0 x 10 -28 to approximately
        //     7.9 x 10 28 with 28-29 significant digits.
        Decimal = 15,
        //
        // Summary:
        //     A type representing a date and time value.
        DateTime = 16,
        //
        // Summary:
        //     A sealed class type representing Unicode character strings.
        String = 18
    }
}
我很好奇如何让
c.GetTypeCode()
返回
Boolean
而不是
Char

我试过这个:

char c = true;
System.TypeCode a = c.GetTypeCode();
Console.WriteLine(a);
但这会产生编译时错误:

无法将类型“bool”隐式转换为“char”[Console.NET]csharp(CS0029)


问这个问题只是出于好奇。至少到目前为止,我还没有一个真正需要它的实例。

嗯,
GetTypeCode
返回一种对象类型。如果对象具有
char
类型,则不可能在此处返回其他类型,因为这违反了静态typing@PavelAnikhouski,这又提出了另一个问题。那么
GetTypeCode
GetType
之间有什么区别呢?为什么我们需要
GetTypeCode
?在
IConvertible
接口中声明,实现此接口的每个类型都必须实现此方法。基本上,它用于将值转换为不同的类型,在
系统中声明和实现。Object
类中,每个对象都应该能够返回其枚举的TypeComments部分为您提供简短的解释,为什么需要它,以及
系统.type
之间的关系,
GetTypeCode
返回对象的类型。如果对象具有
char
类型,则不可能在此处返回其他类型,因为这违反了静态typing@PavelAnikhouski,这又提出了另一个问题。那么
GetTypeCode
GetType
之间有什么区别呢?为什么我们需要
GetTypeCode
?在
IConvertible
接口中声明,实现此接口的每个类型都必须实现此方法。基本上,它用于将值转换为不同的类型,在
系统中声明和实现。对象
类,每个对象都应该能够返回其枚举的TypeComments部分为您提供简短的解释,为什么需要它以及
系统之间的关系。类型