Gets the handle for the current Type.
Assembly: mscorlib (in mscorlib.dll)
Syntax . . :: . TypeHandle
Public Overridable ReadOnly Property TypeHandle As RuntimeTypeHandlepublic virtual RuntimeTypeHandle TypeHandle { get; }public:
virtual property RuntimeTypeHandle TypeHandle {
RuntimeTypeHandle get ();
}abstract TypeHandle : RuntimeTypeHandle
override TypeHandle : RuntimeTypeHandleImplements
_Type Exceptions
| Exception | Condition |
|---|---|
| NotSupportedException | The .NET Compact Framework does not currently support this property. |
Examples
The following example returns the handle of the corresponding type and passes the handle to a method that gets the type from the handle and displays it.
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic
Class [MyClass]
Public myField As Integer = 10
End Class '[MyClass]
Class Type_TypeHandle
Public Shared Sub Main()
Try
Dim [myClass] As New [MyClass]()
' Get the type of MyClass.
Dim myClassType As Type = [myClass].GetType()
' Get the runtime handle of MyClass.
Dim myClassHandle As RuntimeTypeHandle = myClassType.TypeHandle
DisplayTypeHandle(myClassHandle)
Catch e As Exception
Console.WriteLine("Exception: {0}", e.Message.ToString())
End Try
End Sub 'Main
Public Shared Sub DisplayTypeHandle(ByVal myTypeHandle As RuntimeTypeHandle)
' Get the type from the handle.
Dim myType As Type = Type.GetTypeFromHandle(myTypeHandle)
' Display the type.
Console.WriteLine(ControlChars.NewLine + "Displaying the type from the handle:" + ControlChars.NewLine)
Console.WriteLine("The type is {0}.", myType.ToString())
End Sub 'DisplayTypeHandle
End Class 'Type_TypeHandle
using System;
using System.Reflection;
class MyClass
{
public int myField = 10;
}
class Type_TypeHandle
{
public static void Main()
{
try
{
MyClass myClass = new MyClass();
// Get the type of MyClass.
Type myClassType = myClass.GetType();
// Get the runtime handle of MyClass.
RuntimeTypeHandle myClassHandle = myClassType.TypeHandle;
DisplayTypeHandle(myClassHandle);
}
catch(Exception e)
{
Console.WriteLine("Exception: {0}", e.Message );
}
}
public static void DisplayTypeHandle(RuntimeTypeHandle myTypeHandle)
{
// Get the type from the handle.
Type myType = Type.GetTypeFromHandle(myTypeHandle);
// Display the type.
Console.WriteLine("\nDisplaying the type from the handle:\n");
Console.WriteLine("The type is {0}.", myType.ToString());
}
}
using namespace System;
using namespace System::Reflection;
ref class MyClass
{
public:
int myField;
};
void DisplayTypeHandle( RuntimeTypeHandle myTypeHandle )
{
// Get the type from the handle.
Type^ myType = Type::GetTypeFromHandle( myTypeHandle );
// Display the type.
Console::WriteLine( "\nDisplaying the type from the handle:\n" );
Console::WriteLine( "The type is {0}.", myType );
}
int main()
{
try
{
MyClass^ myClass = gcnew MyClass;
// Get the type of MyClass.
Type^ myClassType = myClass->GetType();
// Get the runtime handle of MyClass.
RuntimeTypeHandle myClassHandle = myClassType->TypeHandle;
DisplayTypeHandle( myClassHandle );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception: {0}", e->Message );
}
}
Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.