.NET Framework Class Library Object..::.GetType Method Updated: December 2010 Gets the Type of the current instance.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)

Syntax
Public Function GetType As Type
member GetType : unit -> Type
Return ValueType: System..::.TypeThe exact runtime type of the current instance.

Remarks
For two objects x and y that have identical runtime types, Object.ReferenceEquals(x.GetType(),y.GetType()) returns true. The following example uses the GetType method with the ReferenceEquals method to determine whether one numeric value is the same type as two other numeric values.
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
Note |
|---|
To determine whether an object is a specific type, you can use your language's type comparison keyword or construct. For example, you can use the TypeOf…Is construct in Visual Basic or the is keyword in C#. |
The Type object exposes the metadata associated with the class of the current Object.

Examples
The following code example demonstrates that GetType returns the runtime type of the current instance.
' 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
*/

Version Information
.NET FrameworkSupported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileSupported in: 4, 3.5 SP1 Portable Class LibrarySupported in: Portable Class Library

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

See Also

Change History
Date | History | Reason |
|---|
December 2010
| Added a type comparison example to the Remarks section and added a Note. |
Customer feedback.
|
|
Biblioteca de clases de .NET Framework Object..::.GetType (Método) Obtiene el objeto Type de la instancia actual.
Espacio de nombres:
System
Ensamblado:
mscorlib (en mscorlib.dll)

Sintaxis
Public Function GetType As Type
member GetType : unit -> Type
Valor devueltoTipo: System..::.TypeTipo en tiempo de ejecución de la instancia actual.

Comentarios
En el caso de dos objetos x e y que tengan tipos idénticos en tiempo de ejecución, Object.ReferenceEquals(x.GetType(),y.GetType()) devolverá true. El siguiente ejemplo utiliza el método GetType con el método ReferenceEquals para determinar si un valor numérico es el mismo tipo que otros dos valores numéricos.
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 |
|---|
Para determinar si un objeto es un tipo específico, puede utilizar la palabra clave de comparación de tipo de su lenguaje o construcción. Por ejemplo, puede utilizar la construcción TypeOf…Is en Visual Basic o la palabra clave is en C#. |
El objeto Type expone los metadatos asociados a la clase del Object actual.

Ejemplos
En el siguiente ejemplo de código se muestra que GetType devuelve el tipo en tiempo de ejecución de la instancia actual.
' 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
*/

Información de versión
.NET FrameworkCompatible con: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileCompatible con: 4, 3.5 SP1 Compatible con:

Plataformas
Windows 7, Windows Vista SP1 o posterior, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (no se admite Server Core), Windows Server 2008 R2 (se admite Server Core con SP1 o posterior), Windows Server 2003 SP2
.NET Framework no admite todas las versiones de todas las plataformas. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

Vea también

Historial de cambios
Fecha | Historial | Motivo |
|---|
Diciembre de 2010
| Agregado un ejemplo de comparación de tipos a la sección Notas y agregada una nota. |
Comentarios de los clientes.
|
|