Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método Object.GetType ()

 

Obtiene el Type de la instancia actual.

Espacio de nombres:   System
Ensamblado:  mscorlib (en mscorlib.dll)

Public Function GetType As Type

Valor devuelto

Type: System.Type

El tipo en tiempo de ejecución exacto de la instancia actual.

Because T:System.Object is the base class for all types in the .NET Framework type system, the M:System.Object.GetType method can be used to return T:System.Type objects that represent all .NET Framework types. The .NET Framework recognizes the following five categories of types:

  • Classes, which are derived from T:System.Object,

  • Value types, which are derived from T:System.ValueType.

  • Interfaces, which are derived from T:System.Object starting with the .NET Framework 2.0.

  • Enumerations, which are derived from T:System.Enum.

  • Delegates, which are derived from T:System.MulticastDelegate.

For two objects x and y that have identical runtime types, Object.ReferenceEquals(x.GetType(),y.GetType()) returns true. The following example uses the M:System.Object.GetType method with the M:System.Object.ReferenceEquals(System.Object,System.Object) method to determine whether one numeric value is the same type as two other numeric values.

Module Example
   Public Sub Main()
      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()))
   End Sub
End Module
' The example displays the following output:
'       n1 and n2 are the same type: True
'       n1 and n3 are the same type: False      
System_CAPS_noteNota

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 M:System.Object.GetType method is inherited by all types that derive from T:System.Object. This means that, in addition to using your own language's comparison keyword, you can use the M:System.Object.GetType method to determine the type of a particular object, as the following example shows.

Module Example
   Public Sub Main()
      Dim values() As Object = { 12, CLng(10653), CByte(12), 
                                 CSbyte(-5), 16.3, "string" } 
      For Each value In values
         Dim t AS Type = value.GetType()
         If t.Equals(GetType(Byte))
            Console.WriteLine("{0} is an unsigned byte.", value)
         ElseIf t.Equals(GetType(SByte))
            Console.WriteLine("{0} is a signed byte.", value)
         ElseIf t.Equals(GetType(Integer))   
            Console.WriteLine("{0} is a 32-bit integer.", value)
         ElseIf t.Equals(GetType(Long))   
            Console.WriteLine("{0} is a 32-bit integer.", value)
         ElseIf t.Equals(GetType(Double))
            Console.WriteLine("{0} is a double-precision floating point.", 
                              value)
         Else
            Console.WriteLine("'{0}' is another data type.", value)
         End If   
      Next      
   End Sub
End Module
' The example displays the following output:
'       12 is a 32-bit integer.
'       10653 is a 32-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.

The T:System.Type object exposes the metadata associated with the class of the current T:System.Object.

The following code example demonstrates that M:System.Object.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

Plataforma universal de Windows
Disponible desde 8
.NET Framework
Disponible desde 1.1
Biblioteca de clases portable
Se admite en: plataformas portátiles de .NET
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Windows Phone
Disponible desde 8.1
Volver al principio
Mostrar: