共用方式為


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 列舉。 在 方法內的 WriteObjectInfo 決策區塊中, TypeCode 會檢查 參數的 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 類型,會傳回基礎整數型別的類型代碼。

適用於

另請參閱