Metodo Object.GetType (System)

Cambia visualizzazione:
ScriptFree
Riferimento a .NET Framework
Metodo Object.GetType
Il presente articolo è stato tradotto manualmente. Per visualizzare questa pagina e contemporaneamente visualizzarne il contenuto in lingua inglese, passare alla visualizzazione semplificata.

Ottiene l'oggetto Type dell'istanza corrente.

Spazio dei nomi:  System
Assembly:  mscorlib (in mscorlib.dll)
Sintassi

Visual Basic
Public Function GetType As Type
C#
public Type GetType()
Visual C++
public:
Type^ GetType()
F#
member GetType : unit -> Type 

Valore restituito

Tipo: System.Type
Tipo in fase di esecuzione esatto dell'istanza corrente.
Note

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

Visual Basic

Dim n1 As Integer = 12
Dim n2 As Integer = 82
Dim n3 As Long = 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      


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 Nota

Per determinare se un oggetto è un tipo specifico, è possibile utilizzare il costrutto o la parola chiave di conversione dei tipi della lingua. Ad esempio è possibile utilizzare il costrutto TypeOf…Is in Visual Basic o la parola chiave is in C#.

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

Esempi

Nell'esempio di codice che segue si dimostra che GetType restituisce il tipo in fase di esecuzione dell'istanza corrente.

Visual Basic

' Define a base and a derived class.
Public Class MyBaseClass
End Class 

Public Class MyDerivedClass : Inherits MyBaseClass
End Class 

Public Class Test
    Public Shared Sub Main() 
        Dim base As New MyBaseClass()
        Dim derived As New MyDerivedClass()
        Dim o As Object = derived
        Dim b As MyBaseClass = derived

        Console.WriteLine("base.GetType returns {0}", base.GetType())
        Console.WriteLine("derived.GetType returns {0}", derived.GetType())
        Console.WriteLine("Dim o As Object = derived; o.GetType returns {0}", o.GetType())
        Console.WriteLine("Dim b As MyBaseClass = derived; b.Type returns {0}", b.GetType())
    End Sub 
End Class 
' The example displays the following output:
'    base.GetType returns MyBaseClass
'    derived.GetType returns MyDerivedClass
'    Dim o As Object = derived; o.GetType returns MyDerivedClass
'    Dim b As MyBaseClass = derived; b.Type returns MyDerivedClass


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 


Visual C++

using namespace System;

public ref class MyBaseClass {};

public ref class MyDerivedClass: MyBaseClass{};

int main()
{
   MyBaseClass^ myBase = gcnew MyBaseClass;
   MyDerivedClass^ myDerived = gcnew 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() );
}

/*

This code produces the following output.

mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass 

*/


Informazioni sulla versione

.NET Framework

Supportato in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supportato in: 4, 3.5 SP1

Supportato in:
Piattaforme

Windows 7, Windows Vista SP1 o versione successiva, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (componenti di base del server non supportati), Windows Server 2008 R2 (componenti di base del server supportati con SP1 o versione successiva), Windows Server 2003 SP2

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.
Vedere anche

Riferimenti

Cronologia delle modifiche

Data

Cronologia

Motivo

Dicembre 2010

È stato aggiunto un esempio di confronto tra tipi alla sezione relativa alle osservazioni ed è stata aggiunta una nota.

Commenti e suggerimenti dei clienti.