Type.GetTypeHandle() Method in C#


The Type.GetTypeHandle() method in C# is used to get the handle for the Type of a specified object.

Syntax

Following is the syntax −

public static RuntimeTypeHandle GetTypeHandle (object ob);

Above, ob is the object for which to get the type handle.

Example

using System;
public class Demo {
   public static void Main(){
      Type type1 = typeof(System.Type);
      RuntimeTypeHandle typeHandle = Type.GetTypeHandle(type1);
      Type type = Type.GetTypeFromHandle(typeHandle);
      Console.WriteLine("Attributes = " + type.Attributes);
      Console.WriteLine("Type Referenced = "+ type);
   }
}

Output

This will produce the following output −

Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit
Type Referenced = System.RuntimeType

Example

Let us now see another example to implement the Type.GetTypeHandle() method −

using System;
public class Demo {
   public static void Main(){
      Type type1 = typeof(double);
      RuntimeTypeHandle typeHandle = Type.GetTypeHandle(type1);
      Type type = Type.GetTypeFromHandle(typeHandle);
      Console.WriteLine("Attributes = " + type.Attributes);
      Console.WriteLine("Type Referenced = "+ type);
   }
}

Output

This will produce the following output −

Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit
Type Referenced = System.RuntimeType

Updated on: 05-Nov-2019

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements