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

Lấy interface cụ thể được triển khai bởi Type hiện tại C#

Ví dụ 1: Kiểu double

using System;

public class Demo {
    public static void Main() {
        Type type = typeof(double);
        Type myInterface = type.GetInterface("IFormattable");
        Console.WriteLine("Interface = " + myInterface);
    }
}

Kết quả

Interface = System.IFormattable

Ví dụ 2: Kiểu float với ignoreCase

using System;

public class Demo {
    public static void Main() {
        Type type = typeof(float);
        Type myInterface = type.GetInterface("IFormattable", true);
        Console.WriteLine("Interface = " + myInterface);
    }
}

Kết quả

Interface = System.IFormattable

Lưu ý: Tham số thứ hai ignoreCase cho phép tìm kiếm không phân biệt chữ hoa/thường.