Phương thức Type.GetInterface() trong C# lấy một interface cụ thể được implement hoặc kế thừa bởi Type hiện tại.
Cú pháp
public Type GetInterface(string name); public abstract Type GetInterface(string name, bool ignoreCase);
Ví dụ 1
using System;
public class Demo
{
public static void Main()
{
Type type = typeof(double);
Type myInterface = type.GetInterface("IFormattable");
Console.WriteLine("Interface = " + myInterface);
}
}
Đầu ra
Interface = System.IFormattable
Ví dụ 2
using System;
public class Demo
{
public static void Main()
{
Type type = typeof(float);
Type myInterface = type.GetInterface("IFormattable", true);
Console.WriteLine("Interface = " + myInterface);
}
}
Đầu ra
Interface = System.IFormattable