Leggere in inglese

Condividi tramite


Object.GetType Metodo

Definizione

Ottiene l'oggetto Type dell'istanza corrente.

C#
public Type GetType();

Restituisce

Tipo esatto di runtime dell'istanza corrente.

Esempio

Nell'esempio di codice seguente viene illustrato che GetType restituisce il tipo di runtime dell'istanza corrente.

C#
using System;

public class MyBaseClass {
}

public class MyDerivedClass: MyBaseClass {
}

public class Test
{
   public static void Main()
   {
      MyBaseClass myBase = new MyBaseClass();
      MyDerivedClass myDerived = new MyDerivedClass();
      object o = myDerived;
      MyBaseClass b = myDerived;

      Console.WriteLine("mybase: Type is {0}", myBase.GetType());
      Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
      Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
      Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
   }
}
// The example displays the following output:
//    mybase: Type is MyBaseClass
//    myDerived: Type is MyDerivedClass
//    object o = myDerived: Type is MyDerivedClass
//    MyBaseClass b = myDerived: Type is MyDerivedClass

Commenti

Poiché System.Object è la classe base per tutti i tipi nel sistema di tipi .NET, il GetType metodo può essere usato per restituire Type oggetti che rappresentano tutti i tipi .NET. .NET riconosce le cinque categorie di tipi seguenti:

Per due oggetti x e y che hanno tipi di runtime identici, Object.ReferenceEquals(x.GetType(),y.GetType()) restituisce true. Nell'esempio seguente viene utilizzato il GetType metodo con il ReferenceEquals metodo per determinare se un valore numerico è lo stesso tipo di altri due valori numerici.

C#
int n1 = 12;
int n2 = 82;
long n3 = 12;

Console.WriteLine("n1 and n2 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n2.GetType()));
Console.WriteLine("n1 and n3 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n3.GetType()));

// The example displays the following output:
//       n1 and n2 are the same type: True
//       n1 and n3 are the same type: False

Nota

Per determinare se un oggetto è un tipo specifico, è possibile usare la parola chiave o il costrutto di confronto dei tipi del linguaggio. Ad esempio, è possibile usare il TypeOf…Is costrutto in Visual Basic o la is parola chiave in C#.

Il GetType metodo viene ereditato da tutti i tipi che derivano da Object. Ciò significa che, oltre a usare la parola chiave di confronto della propria lingua, è possibile usare il GetType metodo per determinare il tipo di un oggetto specifico, come illustrato nell'esempio seguente.

C#
object[] values = { (int) 12, (long) 10653, (byte) 12, (sbyte) -5,
                   16.3, "string" };
foreach (var value in values) {
   Type t = value.GetType();
   if (t.Equals(typeof(byte)))
      Console.WriteLine("{0} is an unsigned byte.", value);
   else if (t.Equals(typeof(sbyte)))
      Console.WriteLine("{0} is a signed byte.", value);
   else if (t.Equals(typeof(int)))
      Console.WriteLine("{0} is a 32-bit integer.", value);
   else if (t.Equals(typeof(long)))
      Console.WriteLine("{0} is a 64-bit integer.", value);
   else if (t.Equals(typeof(double)))
      Console.WriteLine("{0} is a double-precision floating point.",
                        value);
   else
      Console.WriteLine("'{0}' is another data type.", value);
}

// The example displays the following output:
//    12 is a 32-bit integer.
//    10653 is a 64-bit integer.
//    12 is an unsigned byte.
//    -5 is a signed byte.
//    16.3 is a double-precision floating point.
//    'string' is another data type.

L'oggetto Type espone i metadati associati alla classe dell'oggetto corrente Object.

Si applica a

Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Vedi anche