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.GetTypeHandle() Method in C#
The Type.GetTypeHandle() method in C# is used to get the runtime type handle for a specified object. A RuntimeTypeHandle provides a lightweight way to reference a type without holding a direct reference to the Type object itself, which is useful in performance-critical scenarios and interop operations.
Syntax
Following is the syntax for the Type.GetTypeHandle() method −
public static RuntimeTypeHandle GetTypeHandle(object o)
Parameters
o − The object for which to get the type handle.
Return Value
Returns a RuntimeTypeHandle structure that represents the type of the specified object.
Using Type.GetTypeHandle() with System Types
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);
}
}
The output of the above code is −
Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit Type Referenced = System.RuntimeType
Using Type.GetTypeHandle() with Primitive Types
Example
using System;
public class Demo {
public static void Main() {
double value = 42.5;
RuntimeTypeHandle typeHandle = Type.GetTypeHandle(value);
Type type = Type.GetTypeFromHandle(typeHandle);
Console.WriteLine("Attributes = " + type.Attributes);
Console.WriteLine("Type Referenced = " + type);
Console.WriteLine("Full Name = " + type.FullName);
}
}
The output of the above code is −
Attributes = AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, Serializable, BeforeFieldInit Type Referenced = System.Double Full Name = System.Double
Using Type.GetTypeHandle() with Custom Objects
Example
using System;
class Student {
public string Name { get; set; }
public int Age { get; set; }
}
public class Demo {
public static void Main() {
Student student = new Student { Name = "John", Age = 20 };
RuntimeTypeHandle typeHandle = Type.GetTypeHandle(student);
Type type = Type.GetTypeFromHandle(typeHandle);
Console.WriteLine("Type Name = " + type.Name);
Console.WriteLine("Assembly = " + type.Assembly.GetName().Name);
Console.WriteLine("Is Class = " + type.IsClass);
}
}
The output of the above code is −
Type Name = Student Assembly = testAssembly Is Class = True
Common Use Cases
Performance optimization −
RuntimeTypeHandlehas less memory overhead thanTypeobjects.Interop scenarios − When working with unmanaged code that requires type handles.
Type comparison − Efficient way to compare types without full
Typeobject allocation.
Conclusion
The Type.GetTypeHandle() method provides an efficient way to obtain a RuntimeTypeHandle for any object. This handle can be used for performance-critical operations and can be converted back to a Type object using Type.GetTypeFromHandle() when needed.
