Get the handle for the Type of a specified object C#

CsharpServer Side ProgrammingProgramming

To get the handle for the Type of a specified object, the code is as follows −

Example

 Live Demo

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 see another example −

 Live Demo

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
raja
Published on 10-Dec-2019 09:40:56
Advertisements