เมธอด Type.GetInterface() ใน C# ใช้ดึงอินเทอร์เฟซเฉพาะที่ถูกนำไปใช้หรือสืบทอดโดย Type ปัจจุบัน
ไวยากรณ์
public Type GetInterface (string name); public abstract Type GetInterface (string name, bool ignoreCase);
ตัวอย่าง
ตัวอย่างที่ 1: ดึงอินเทอร์เฟซ IFormattable ของ double
using System;
public class Demo {
public static void Main(){
Type type = typeof(double);
Type myInterface = type.GetInterface("IFormattable");
Console.WriteLine("Interface = " + myInterface);
}
}
ผลลัพธ์
Interface = System.IFormattable
ตัวอย่างเพิ่มเติม
ตัวอย่างที่ 2: ดึงอินเทอร์เฟซ IFormattable ของ float พร้อม ignoreCase
using System;
public class Demo {
public static void Main(){
Type type = typeof(float);
Type myInterface = type.GetInterface("IFormattable", true);
Console.WriteLine("Interface = " + myInterface);
}
}
ผลลัพธ์
Interface = System.IFormattable