Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Type.TypeHandle Property

Gets the handle for the current Type.

[Visual Basic]
Public MustOverride ReadOnly Property TypeHandle As _
   RuntimeTypeHandle
[C#]
public abstract RuntimeTypeHandle TypeHandle {get;}
[C++]
public: __property virtual RuntimeTypeHandle get_TypeHandle() = 0;
[JScript]
public abstract function get TypeHandle() : RuntimeTypeHandle;

Property Value

The handle for the current Type.

Remarks

TypeHandle encapsulates a pointer to an internal data structure that represents the type. This handle is unique during the process lifetime. The handle is valid only in the application domain in which it was obtained.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
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

[C#] 
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());
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

__gc 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(S"\nDisplaying the type from the handle:\n");
   Console::WriteLine(S"The type is {0}.", myType);
}

int 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(S"Exception: {0}", e->Message);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace | RuntimeTypeHandle | GetTypeHandle | GetTypeFromHandle

Show:
© 2015 Microsoft