Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Type.GetTypeFromHandle() trong C#

Phương thức Type.GetTypeFromHandle() trong C# được dùng để lấy kiểu (Type) được tham chiếu bởi handle (RuntimeTypeHandle) được chỉ định.

Cú pháp

public static Type GetTypeFromHandle(RuntimeTypeHandle handle);

Tham số handle là đối tượng tham chiếu đến kiểu dữ liệu.

Ví dụ 1

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("Attributes = " + type.Attributes);
    }
}

Kết quả

Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit

Ví dụ 2

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);
    }
}

Kết quả

Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit