Export (0) Print
Expand All
Expand Minimize

Type.GetTypeHandle Method

Gets the handle for the Type of a specified object.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function GetTypeHandle ( _
	o As Object _
) As RuntimeTypeHandle
'Usage
Dim o As Object
Dim returnValue As RuntimeTypeHandle

returnValue = Type.GetTypeHandle(o)
public static RuntimeTypeHandle GetTypeHandle (
	Object o
)
public static function GetTypeHandle (
	o : Object
) : RuntimeTypeHandle
Not applicable.

Parameters

o

The Object for which to get the Type handle.

Return Value

The handle for the Type of the specified Object.

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

The following example defines the class MyClass1, gets an instance of it, and retrieves the runtime handle of the object.

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

import System.*;
import System.Reflection.*;

public class MyClass1
{
    private int x = 0;

    public int MyMethod()
    {
        return x;
    } //MyMethod
} //MyClass1

public class MyClass2
{
    public static void main(String[] args)
    {
        MyClass1 myClass1 = new MyClass1();
        // Get the RuntimeTypeHandle from an object.
        RuntimeTypeHandle myRTHFromObject = Type.GetTypeHandle(myClass1);
        // Get the RuntimeTypeHandle from a type.
        RuntimeTypeHandle myRTHFromType = (MyClass1.class.ToType()).
            get_TypeHandle();

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft