Export (0) Print
Expand All

Type.GetTypeHandle Method

Gets the handle for the Type of a specified object.

[Visual Basic]
Public Shared Function GetTypeHandle( _
   ByVal o As Object _
) As RuntimeTypeHandle
[C#]
public static RuntimeTypeHandle GetTypeHandle(
 object o
);
[C++]
public: static RuntimeTypeHandle GetTypeHandle(
 Object* o
);
[JScript]
public static function GetTypeHandle(
   o : Object
) : RuntimeTypeHandle;

Parameters

o
The Object for which to get the Type handle.

Return Value

The handle for the Type of the specified Object.

Remarks

The handles are valid only in the application domain in which they were obtained.

Example

[Visual Basic, C#, C++] The following example defines the class MyClass1, gets an instance of it, and retrieves the runtime handle of the object.

[Visual Basic] 
Imports System
Imports System.Reflection

 _
Public Class MyClass1
   Private x As Integer = 0
   
   Public Function MyMethod() As Integer
      Return x
   End Function 'MyMethod
End Class 'MyClass1
 _
Public Class MyClass2
   
   Public Shared Sub Main()
      Dim myClass1 As New MyClass1()
      
      ' Get the RuntimeTypeHandle from an object.
      Dim myRTHFromObject As RuntimeTypeHandle = Type.GetTypeHandle(myClass1)
      ' Get the RuntimeTypeHandle from a type.
      Dim myRTHFromType As RuntimeTypeHandle = GetType(MyClass1).TypeHandle
      
      Console.WriteLine("myRTHFromObject.Value: {0}", myRTHFromObject.Value)
      Console.WriteLine("myRTHFromObject.Type: {0}", myRTHFromObject.GetType())
      Console.WriteLine("myRTHFromType.Value: {0}", myRTHFromType.Value)
      Console.WriteLine("myRTHFromType.Type: {0}", myRTHFromType.GetType())
   End Sub 'Main 
End Class 'MyClass2

[C#] 
using System;
using System.Reflection;
public class MyClass1
{
    private int x=0;
    public int MyMethod()
    {
        return x;
    }
}
public class MyClass2
{
    public static void Main()
    {
        MyClass1 myClass1 = new MyClass1();

        // Get the RuntimeTypeHandle from an object.
        RuntimeTypeHandle myRTHFromObject = Type.GetTypeHandle(myClass1);
        // Get the RuntimeTypeHandle from a type.
        RuntimeTypeHandle myRTHFromType = typeof(MyClass1).TypeHandle;

        Console.WriteLine("myRTHFromObject.Value: {0}", myRTHFromObject.Value);
        Console.WriteLine("myRTHFromObject.Type: {0}", myRTHFromObject.GetType());
        Console.WriteLine("myRTHFromType.Value: {0}", myRTHFromType.Value);
        Console.WriteLine("myRTHFromType.Type: {0}", myRTHFromType.GetType());

    }
}

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

using namespace System;
using namespace System::Reflection;

public __gc class MyClass1 {
private:
   int x;
public:
   int MyMethod() {
      return x;
   }
};

int main() {
   MyClass1* myClass1 = new MyClass1();

   // Get the RuntimeTypeHandle from an object.
   RuntimeTypeHandle myRTHFromObject = Type::GetTypeHandle(myClass1);
   // Get the RuntimeTypeHandle from a type.
   RuntimeTypeHandle myRTHFromType = __typeof(MyClass1)->TypeHandle;

   Console::WriteLine(S"myRTHFromObject.Value: {0}",__box( myRTHFromObject.Value));
   Console::WriteLine(S"myRTHFromObject.Type: {0}", __box(myRTHFromObject)->GetType());
   Console::WriteLine(S"myRTHFromType.Value: {0}",__box( myRTHFromType.Value));
   Console::WriteLine(S"myRTHFromType.Type: {0}", __box(myRTHFromType)->GetType());

}

[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, Common Language Infrastructure (CLI) Standard

See Also

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

Show:
© 2014 Microsoft