获取 C# 中当前 Type 实现或继承的特定接口


若要取得當前 Type 實作或繼承的特定介面,請採用以下程式碼 −

範例

 線上展示

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

範例

讓我們看另一個範例 −

 線上展示

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

更新日期:2019-12-11

87 次瀏覽

啟動你的 職涯

完成课程后获得认证

开始
广告
© . All rights reserved.