Ottiene l'oggetto Type dell'istanza corrente.
Assembly: mscorlib (in mscorlib.dll)
Public Function GetType As Type
public Type GetType()
public:
Type^ GetType()
member GetType : unit -> Type
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.
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
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 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.
Nell'esempio di codice che segue si dimostra che GetType restituisce il tipo in fase di esecuzione dell'istanza corrente.
' 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
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
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 */
.NET Framework
Supportato in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supportato in: 4, 3.5 SP1Supportato in:
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.
Riferimenti
|
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. |
Nota