Gets the Type of the current instance.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function GetType As Type
Dim instance As Object
Dim returnValue As Type
returnValue = instance.GetType()
public function GetType() : Type
Return Value
Type:
System..::.TypeThe 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.
Imports System
' 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 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
'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
'
using System;
public class MyBaseClass: Object {
}
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());
}
}
/*
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
*/
using namespace System;
public ref class MyBaseClass: public Object{};
public ref class MyDerivedClass: public 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
*/
import System
public class MyBaseClass extends Object {
}
public class MyDerivedClass extends MyBaseClass {
}
public class Test {
public static function Main() {
var myBase : MyBaseClass = new MyBaseClass();
var myDerived : MyDerivedClass = new MyDerivedClass();
var o = myDerived;
var b : MyBaseClass = 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());
}
}
Test.Main();
/*
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
*/
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference