Type.GetInterface()方法用于获取由当前Type实现或继承的特定接口。
- GetInterface(String)方法
- GetInterface(String,Boolean)方法
GetInterface(String)方法
此方法用于搜索具有指定名称的接口。
Syntax: public Type GetInterface (string name);
Here, it takes the string containing the name of the interface to get. For generic interfaces, this is the mangled name.
Return Value: This method returns an object representing the interface with the specified name, implemented or inherited by the current Type, if found otherwise, null.
Exception: This method throws ArgumentNullException if the name is null .
下面的程序说明了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)方法
此方法用于搜索指定的接口,指定在派生类中重写时是否对接口名称进行不区分大小写的搜索。
Syntax: public abstract Type GetInterface (string name, bool ignoreCase);
Parameters:
name: The string containing the name of the interface to get. For generic interfaces, this is the mangled name.
ignoreCase: true to ignore the case of that part of the name which specifies the simple interface name (the part that specifies the namespace must be correctly cased) Or false to perform a case-sensitive search for all parts of the name.
返回值:该方法返回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