C# Type GetTypeHandle
Description
Type GetTypeHandle
gets the handle for the Type of a specified
object.
Syntax
Type.GetTypeHandle
has the following syntax.
public static RuntimeTypeHandle GetTypeHandle(
Object o
)
Parameters
Type.GetTypeHandle
has the following parameters.
o
- The object for which to get the type handle.
Returns
Type.GetTypeHandle
method returns The handle for the Type of the specified Object.
Example
The following example defines the class MyClass1, gets an instance of it, and retrieves the runtime handle of the object.
// ww w . j a va2s. c o m
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();
RuntimeTypeHandle myRTHFromObject = Type.GetTypeHandle(myClass1);
RuntimeTypeHandle myRTHFromType = typeof(MyClass1).TypeHandle;
Console.WriteLine("myRTHFromObject.GetType(): {0}", myRTHFromObject.GetType());
Console.WriteLine(Type.GetTypeFromHandle(myRTHFromObject));
}
}
The code above generates the following result.