Type.GetTypeHandle() วิธีการใน C # ถูกใช้เพื่อรับหมายเลขอ้างอิงสำหรับประเภทของวัตถุที่ระบุ
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
public static RuntimeTypeHandle GetTypeHandle (object ob);
ด้านบน ob คืออ็อบเจ็กต์สำหรับรับตัวจัดการประเภท
ตัวอย่าง
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);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit Type Referenced = System.RuntimeType
ตัวอย่าง
ให้เราดูตัวอย่างอื่นเพื่อนำวิธีการ Type.GetTypeHandle() ไปใช้ -
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);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit Type Referenced = System.RuntimeType