System Namespace Object Class
Returns the Type of the current instance.
[ VB ]
<Serializable>
<ClassInterface ( ClassInterfaceType.AutoDual ) >
Public Function GetType ( ) As Type
[ C# ]
[ Serializable ]
[ ClassInterface ( ClassInterfaceType.AutoDual ) ]
public Type GetType ( );
[ C++ ]
[ Serializable ]
[ ClassInterface ( ClassInterfaceType.AutoDual ) ]
public: Type* GetType ( );
[ JScript ]
public Serializable
ClassInterface ( ClassInterfaceType.AutoDual )
function GetType ( ) : 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.
[ C#, JScript ] The following code example demonstrates that GetType returns the runtime type of the current instance.
[ C# ]
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;
Response.Write ( "mybase: Type is {0}", myBase.GetType ( ) );
Response.Write ( "myDerived: Type is {0}", myDerived.GetType ( ) );
Response.Write ( "object o = myDerived: Type is {0}", o.GetType ( ) );
Response.Write ( "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
*/
[ JScript ]
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;
Response.Write ( "mybase: Type is {0}", myBase.GetType ( ) );
Response.Write ( "myDerived: Type is {0}", myDerived.GetType ( ) );
Response.Write ( "object o = myDerived: Type is {0}", o.GetType ( ) );
Response.Write ( "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
*/
Object Members Type