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
Type.GetTypeFromHandle() Method in C#
The Type.GetTypeFromHandle() method in C# is used to get the Type object referenced by the specified RuntimeTypeHandle. This method is particularly useful when working with type handles obtained from reflection operations or when you need to convert a type handle back to its corresponding Type object.
Syntax
Following is the syntax −
public static Type GetTypeFromHandle(RuntimeTypeHandle handle);
Parameters
The method accepts the following parameter −
-
handle − A
RuntimeTypeHandlethat refers to a specific type.
Return Value
Returns a Type object that represents the type referenced by the handle, or null if the handle is invalid.
Using GetTypeFromHandle() with Primitive 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, Serializable, BeforeFieldInit
Using GetTypeFromHandle() with System 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("Is Value Type: " + type.IsValueType);
Console.WriteLine("Is Class: " + type.IsClass);
Console.WriteLine("Namespace: " + type.Namespace);
}
}
The output of the above code is −
Type Name: String Is Value Type: False Is Class: True Namespace: System
Using GetTypeFromHandle() with Custom Classes
Example
using System;
class Employee {
public string Name { get; set; }
public int Id { get; set; }
}
public class Demo {
public static void Main() {
Type employeeType = typeof(Employee);
RuntimeTypeHandle handle = Type.GetTypeHandle(employeeType);
Type retrievedType = Type.GetTypeFromHandle(handle);
Console.WriteLine("Type Name: " + retrievedType.Name);
Console.WriteLine("Assembly: " + retrievedType.Assembly.GetName().Name);
Console.WriteLine("Methods Count: " + retrievedType.GetMethods().Length);
Console.WriteLine("Properties Count: " + retrievedType.GetProperties().Length);
}
}
The output of the above code is −
Type Name: Employee Assembly: source Methods Count: 9 Properties Count: 2
Common Use Cases
-
Reflection scenarios where you have a
RuntimeTypeHandleand need to accessTypemethods. -
Serialization and deserialization processes that work with type handles.
-
Performance optimization when repeatedly accessing the same type information.
-
Interoperability with unmanaged code that returns type handles.
Conclusion
The Type.GetTypeFromHandle() method provides a way to convert a RuntimeTypeHandle back to its corresponding Type object. This method is essential in reflection scenarios and enables access to all type information and metadata once you have a type handle.
