Type.GetTypeCode(Type) 方法

定义

获取指定 Type 的基础类型代码。

public:
 static TypeCode GetTypeCode(Type ^ type);
public static TypeCode GetTypeCode (Type? type);
public static TypeCode GetTypeCode (Type type);
static member GetTypeCode : Type -> TypeCode
Public Shared Function GetTypeCode (type As Type) As TypeCode

参数

type
Type

要获取其基础代码的类型。

返回

如果 Emptytype,则为基础类型代码或 null

示例

下面的代码示例演示如何 TypeCode 使用 枚举。 在 方法内的决策块中 WriteObjectInfoTypeCode 检查参数的 Object ,并将相应的消息写入控制台。

void WriteObjectInfo( Object^ testObject )
{
   TypeCode typeCode = Type::GetTypeCode( testObject->GetType() );
   switch ( typeCode )
   {
      case TypeCode::Boolean:
         Console::WriteLine( "Boolean: {0}", testObject );
         break;

      case TypeCode::Double:
         Console::WriteLine( "Double: {0}", testObject );
         break;

      default:
         Console::WriteLine( "{0}: {1}", typeCode, testObject );
         break;
   }
}
static void WriteObjectInfo(object testObject)
{
    TypeCode    typeCode = Type.GetTypeCode( testObject.GetType() );

    switch( typeCode )
    {
        case TypeCode.Boolean:
            Console.WriteLine("Boolean: {0}", testObject);
            break;

        case TypeCode.Double:
            Console.WriteLine("Double: {0}", testObject);
            break;

        default:
            Console.WriteLine("{0}: {1}", typeCode.ToString(), testObject);
            break;
    }
}
let writeObjectInfo (testObject: obj) = 
    let typeCode = Type.GetTypeCode(testObject.GetType())
    match typeCode with
    | TypeCode.Boolean ->
        printfn $"Boolean: {testObject}"
    | TypeCode.Double ->
        printfn "Double: {testObject}"
    | _ ->
        printfn $"{typeCode}: {testObject}"
Sub WriteObjectInfo(ByVal testObject As Object)
    Dim typeCode As TypeCode = Type.GetTypeCode(testObject.GetType())

    Select Case typeCode
        Case typeCode.Boolean
            Console.WriteLine("Boolean: {0}", testObject)

        Case typeCode.Double
            Console.WriteLine("Double: {0}", testObject)

        Case Else
            Console.WriteLine("{0}: {1}", typeCode.ToString(), testObject)
    End Select
End Sub

注解

Type继承时,可以通过重写 GetTypeCodeImpl 方法来更改此方法的行为。 对于 Enum 类型,返回基础整型类型的类型代码。

适用于

另请参阅