Type.GetTypeCode(Type) Metodo

Definizione

Ottiene il codice di tipo sottostante dell'oggetto specificato 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

Parametri

type
Type

Tipo il cui codice di tipo sottostante da ottenere.

Valori restituiti

Codice del tipo sottostante o Empty se type è null.

Esempio

Nell'esempio di codice seguente viene illustrato come usare l'enumerazione TypeCode . In un blocco decisionale all'interno del WriteObjectInfo metodo viene esaminato l'oggetto TypeCode di un Object parametro e viene scritto un messaggio appropriato nella console.

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

Commenti

Quando si eredita da Type, è possibile modificare il comportamento di questo metodo eseguendo l'override del GetTypeCodeImpl metodo . Per Enum i tipi, viene restituito il codice di tipo del tipo integrale sottostante.

Si applica a

Vedi anche