Type.FindInterfaces(TypeFilter,Object)方法用於返回Type對象數組,該對象表示由當前Type實現或繼承的接口的過濾列表。在搜索過程中,將考慮由此類實現的所有接口,無論是由基類聲明還是由此類本身聲明。
此方法搜索基類層次結構,返回每個類實現的每個匹配接口以及每個接口實現的所有匹配接口(即,返回匹配接口的可傳遞閉包)。沒有返回重複的接口。
用法:
public virtual Type[] FindInterfaces (System.Reflection.TypeFilter filter, object filterCriteria);
參數:
- filter:將接口與filterCriteria比較的委托。
- filterCriteria:確定是否在返回的數組中包含接口的搜索條件。
返回值:此方法返回一個Type對象數組,該數組表示由當前Type實現或繼承的接口的過濾列表;如果當前Type沒有實現或繼承與該過濾器匹配的接口,則返回一個Type類型的空數組。
異常:如果filter為null,則此方法引發ArgumentNullException。
以下示例程序旨在說明上述方法的用法:
示例1:
// C# program to demonstrate the
// Type.FindInterfaces(TypeFilter,
// Object) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Creating try-catch block
// to handle exceptions
try {
// Declaring and initializing
// object Type Datatype
Type type = typeof(System.String);
// Declaring and initializing the object
// of TypeFilter Datatype which help the
// delegate that compares the interfaces
// against filterCriteria
TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);
// Declaring and initializing filterCriteria
// object. It is used to search the criteria
// that determines whether an interface should
// be included in the returned array.
object filterCriteria = "System.Collections.IEnumerable";
// Getting the filtered list of interface
// using FindInterfaces() method
Type[] myInterfaces = type.FindInterfaces(myFilter,
filterCriteria);
// Display the interfaces
for (int j = 0; j < myInterfaces.Length; j++)
Console.WriteLine("filtered list of interface : {0}.",
myInterfaces[j].ToString());
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining MyInterfaceFilter
// which helps to fix the certain
// condition on which filtration
// took place
public static bool MyInterfaceFilter(Type typeObj,
Object criteriaObj)
{
if (typeObj.ToString() == criteriaObj.ToString())
return true;
else
return false;
}
}
輸出:
filtered list of interface : System.Collections.IEnumerable.
示例2:
// C# program to demonstrate the
// Type.FindInterfaces(TypeFilter,
// Object) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Creating try-catch block
// to handle exceptions
try {
// Declaring and initializing
// object Type Datatype
Type type = typeof(System.String);
// Declaring and initializing object
// of TypeFilter Datatype
// which help the delegate that compares
// the interfaces against filterCriteria.
TypeFilter myFilter = null;
// Declaring and initializing filterCriteria
// object. It is used to search the criteria
// that determines whether an interface should
// be included in the returned array.
object filterCriteria = "System.Collections.IEnumerable";
// Getting the filtered list of interface
// using FindInterfaces() method
Type[] myInterfaces = type.FindInterfaces(myFilter,
filterCriteria);
// Display the interfaces
for (int j = 0; j < myInterfaces.Length; j++)
Console.WriteLine("filtered list of interface : {0}.",
myInterfaces[j].ToString());
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("myFilter should not be null");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
myFilter should not be null Exception Thrown: System.ArgumentNullException
參考:
相關用法
- C# MathF.Tan()用法及代碼示例
- C# MathF.Abs()用法及代碼示例
- C# MathF.Max()用法及代碼示例
- C# MathF.Log()用法及代碼示例
- C# MathF.Min()用法及代碼示例
- C# MathF.Cos()用法及代碼示例
- C# MathF.Exp()用法及代碼示例
- C# MathF.Pow()用法及代碼示例
- C# MathF.Sin()用法及代碼示例
- C# UInt16.GetHashCode用法及代碼示例
- C# Single.IsNegative()用法及代碼示例
- C# Single.IsFinite()用法及代碼示例
- C# Single.CompareTo()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Type.FindInterfaces() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。