เมธอด Type.GetInterface() ใน C# ใช้เพื่อรับอินเทอร์เฟซเฉพาะที่ใช้งานหรือสืบทอดโดย Type ปัจจุบัน
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
public Type GetInterface (string name); public abstract Type GetInterface (string name, bool ignoreCase);
ตัวอย่าง
ให้เราดูตัวอย่างการใช้เมธอด Type.GetInterface() -
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
ตัวอย่าง
ให้เราดูตัวอย่างอื่นเพื่อนำวิธีการ Type.GetInterface() ไปใช้ -
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