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 handle for the Type of a specified object C#
To get the handle for the Type of a specified object in C#, you use the Type.GetTypeHandle() method. This method returns a RuntimeTypeHandle structure that represents the type handle, which is a unique identifier for the type in the runtime environment.
A type handle is useful for low-level operations and provides a lightweight way to reference types without keeping the full Type object in memory.
Syntax
Following is the syntax for getting a type handle −
RuntimeTypeHandle handle = Type.GetTypeHandle(objectInstance);
To convert back from handle to Type −
Type type = Type.GetTypeFromHandle(handle);
Parameters
objectInstance − The object whose type handle you want to retrieve
Return Value
Returns a RuntimeTypeHandle structure that represents the type of the specified object.
Using Type Handle with System.Type
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 Handle with Built-in Types
Example
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);
Console.WriteLine("Is Value Type = " + type.IsValueType);
Console.WriteLine("Full Name = " + type.FullName);
}
}
The output of the above code is −
Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit Type Referenced = System.Double Is Value Type = True Full Name = System.Double
Using Type Handle with Custom Objects
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 { 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);
Console.WriteLine("Base Type = " + type.BaseType);
}
}
The output of the above code is −
Type Name = Student Assembly = prog Is Class = True Base Type = System.Object
Conclusion
The Type.GetTypeHandle() method provides a lightweight way to get a runtime handle for any object's type. This handle can be converted back to a Type object using Type.GetTypeFromHandle(), making it useful for efficient type operations and comparisons in performance-critical scenarios.
