Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get the type referenced by the specified type handle in C#
To get the type referenced by the specified type handle in C#, you use the Type.GetTypeFromHandle() method. This method takes a RuntimeTypeHandle and returns the corresponding Type object. Type handles provide a lightweight way to represent types at runtime and are commonly used in low-level reflection scenarios.
Syntax
Following is the syntax for getting a type handle and retrieving the type −
RuntimeTypeHandle typeHandle = Type.GetTypeHandle(object); Type type = Type.GetTypeFromHandle(typeHandle);
Parameters
handle − The
RuntimeTypeHandlestructure that refers to the type.
Return Value
Returns the Type referenced by the specified RuntimeTypeHandle, or null if the handle is invalid.
Using Type.GetTypeFromHandle() with Value Types
Example
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("Type Name = " + type.Name);
Console.WriteLine("Full Name = " + type.FullName);
Console.WriteLine("Attributes = " + type.Attributes);
}
}
The output of the above code is −
Type Name = Int16 Full Name = System.Int16 Attributes = AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, Serializable, BeforeFieldInit
Using Type.GetTypeFromHandle() with Reference Types
Example
using System;
public class Demo {
public static void Main() {
Type type1 = typeof(System.String);
RuntimeTypeHandle typeHandle = Type.GetTypeHandle(type1);
Type type = Type.GetTypeFromHandle(typeHandle);
Console.WriteLine("Type Name = " + type.Name);
Console.WriteLine("Full Name = " + type.FullName);
Console.WriteLine("Is Class = " + type.IsClass);
Console.WriteLine("Is Value Type = " + type.IsValueType);
}
}
The output of the above code is −
Type Name = String Full Name = System.String Is Class = True Is Value Type = False
Using Type.GetTypeFromHandle() with Custom Types
Example
using System;
public class Student {
public string Name { get; set; }
public int Age { get; set; }
}
public class Demo {
public static void Main() {
Student student = new Student();
RuntimeTypeHandle typeHandle = Type.GetTypeHandle(student);
Type type = Type.GetTypeFromHandle(typeHandle);
Console.WriteLine("Type Name = " + type.Name);
Console.WriteLine("Namespace = " + type.Namespace);
Console.WriteLine("Assembly = " + type.Assembly.GetName().Name);
}
}
The output of the above code is −
Type Name = Student Namespace = Assembly = 6be7bb64-7b8b-4c0c-8306-bfa8b8108efc
Common Use Cases
Performance optimization − Type handles are more efficient than storing Type objects in performance-critical scenarios.
Serialization − Type handles can be used to store type information in a compact format.
Reflection caching − Storing type handles instead of Type objects can reduce memory overhead.
Interoperability − Type handles are used in low-level scenarios when working with unmanaged code.
Conclusion
The Type.GetTypeFromHandle() method allows you to retrieve a Type object from a RuntimeTypeHandle. This is useful for performance-critical applications, serialization scenarios, and when working with reflection where type handles provide a more lightweight alternative to storing full Type objects.
