📜  C#| Type.GetInterface()方法

📅  最后修改于: 2021-05-29 15:15:08             🧑  作者: Mango

Type.GetInterface()方法用于获取由当前Type实现或继承的特定接口。

  • GetInterface(String)方法
  • GetInterface(String,Boolean)方法

GetInterface(String)方法

此方法用于搜索具有指定名称的接口。

下面的程序说明了Type.GetInterface(String)方法的用法:

范例1:

// C# program to demonstrate the
// Type.GetInterface(String) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface("IFormattable");
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
interface is: System.IFormattable

示例2:对于ArgumentNullException

// C# program to demonstrate the
// Type.GetInterface(String) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface(null);
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
name is null.
Exception Thrown: System.ArgumentNullException

GetInterface(String,Boolean)方法

此方法用于搜索指定的接口,指定在派生类中重写时是否对接口名称进行不区分大小写的搜索。

返回值:该方法返回n对象,该对象代表具有指定名称的接口,该对象由当前Type实现或继承(如果找到的话),否则返回null。

异常:如果name为null,则此方法将引发ArgumentNullException。

下面的程序说明了上述方法的用法:

范例1:

// C# program to demonstrate the
// Type.GetInterface(String, 
// Boolean) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface("iformattable", true);
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
interface is: System.IFormattable

示例2:对于ArgumentNullException

// C# program to demonstrate the
// Type.GetInterface(String, 
// Boolean) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface(null, true);
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

输出:

name is null.
Exception Thrown: System.ArgumentNullException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getinterface?view=netframework-4.8