Phương thức Type.GetTypeHandle() trong C# được dùng để lấy handle (con trỏ) cho Type của một đối tượng được chỉ định.
Cú pháp
public static RuntimeTypeHandle GetTypeHandle(object ob);
Tham số ob là đối tượng cần lấy type handle.
Ví dụ 1
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);
}
}
Kết quả
Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit Type Referenced = System.RuntimeType
Ví dụ 2
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);
}
}
Kết quả
Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit Type Referenced = System.RuntimeType