当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Type.FindInterfaces()用法及代码示例


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

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Type.FindInterfaces() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。