Get the type referenced by the specified type handle in C#

CsharpServer Side ProgrammingProgramming

To get the type referenced by the specified type handle, the code is as follows −

Example

 Live Demo

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

Output

This will produce the following output −

Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit

Example

Let us see another 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);
   }
}

Output

This will produce the following output −

Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit


raja
Published on 10-Dec-2019 07:35:57
Advertisements