.NET Framework Class Library for Silverlight
Object.GetType Method
Gets the Type of the current instance.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
Visual Basic (Declaration)
<SecuritySafeCriticalAttribute> _ Public Function GetType As Type
C#
[SecuritySafeCriticalAttribute]
public Type GetType()
Return Value
Type: System.TypeThe Type instance that represents the 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 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.
Visual Basic
' 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 '
C#
using System; public class MyBaseClass : Object { } public class MyDerivedClass : MyBaseClass { } public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; MyBaseClass b = myDerived; outputBlock.Text += String.Format("mybase: Type is {0}", myBase.GetType()) + "\n"; outputBlock.Text += String.Format("myDerived: Type is {0}", myDerived.GetType()) + "\n"; outputBlock.Text += String.Format("object o = myDerived: Type is {0}", o.GetType()) + "\n"; outputBlock.Text += String.Format("MyBaseClass b = myDerived: Type is {0}", b.GetType()) + "\n"; } } /* 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
Silverlight
Supported in: 5, 4, 3Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
See Also