Export (0) Print
Expand All

Object.GetType Method

Gets the Type of the current instance.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function GetType As Type

Return Value

Type: System.Type
The Type instance that represents the exact runtime type of the current instance.

For two objects x and y that have identical runtime types, Object.ReferenceEquals(x.GetType(),y.GetType()) returns true.

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

The following code example demonstrates that GetType returns the runtime type of the current instance.



' Example base class and derived class. Note that it
' is not necessary to derive from Object explicitly;
' a class with no Inherits statement implicitly 
' derives from Object.
'
Public Class MyBaseClass
   Inherits Object
End Class

Public Class MyDerivedClass
   Inherits MyBaseClass
End Class

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim base As New MyBaseClass()
      Dim derived As New MyDerivedClass()
      Dim o As Object = derived
      Dim b As MyBaseClass = derived

      outputBlock.Text += String.Format("base.GetType returns {0}", base.GetType()) & vbCrLf
      outputBlock.Text += String.Format("derived.GetType returns {0}", derived.GetType()) & vbCrLf
      outputBlock.Text += String.Format("Dim o As Object = derived; o.GetType returns {0}", o.GetType()) & vbCrLf
      outputBlock.Text += String.Format("Dim b As MyBaseClass = derived; b.Type returns {0}", b.GetType()) & vbCrLf

   End Sub
End Class

'This code example produces 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
'


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft