Computer >> Máy Tính >  >> Lập trình >> C#

Nhận một giao diện cụ thể được triển khai hoặc kế thừa bởi Loại hiện tại trong C #

Để có được một giao diện cụ thể được Loại hiện tại triển khai hoặc kế thừa, mã như sau -

Ví dụ

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

Đầu ra

Điều này sẽ tạo ra kết quả sau -

Interface = System.IFormattable

Ví dụ

Hãy để chúng tôi xem một ví dụ khác -

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

Điều này sẽ tạo ra kết quả sau -

Interface = System.IFormattable