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


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


Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) 方法用于返回指定成员类型的 MemberInfo 对象的过滤数组。
用法:

public virtual System.Reflection.MemberInfo[] FindMembers (System.Reflection.MemberTypes memberType, System.Reflection.BindingFlags bindingAttr, System.Reflection.MemberFilter filter, object filterCriteria); 
 

参数:

  • memberType:它表示应该搜索什么类型的成员。
  • bindingAttr:它用于指定如何进行搜索或零,返回空值。
  • filter:它进行比较,如果当前正在检查的成员与 filterCriteria 匹配,则返回 true,否则返回 false。
  • filterCriteria:确定成员是否在 MemberInfo 对象数组中返回的搜索条件。

以下 BindingFlags 过滤器标志可用于定义要包含在搜索中的成员:

  • 您必须指定 BindingFlags.Instance 或 BindingFlags.Static 才能获得返回。
  • 指定 BindingFlags.Instance 以在搜索中包括实例成员。
  • 指定 BindingFlags.Static 以在搜索中包含静态成员。
  • 指定 BindingFlags.Public 以在搜索中包括公共成员。
  • 指定 BindingFlags.NonPublic 以在搜索中包括非公共成员(即私有成员、内部成员和受保护成员)。

返回值:此方法返回指定成员类型的 MemberInfo 对象的过滤数组。或 MemberInfo 类型的空数组,
异常:如果 filter 为 null,则此方法抛出 ArgumentNullException。示例:

C#


// C# program to demonstrate the
// Type.FindMembers(MemberTypes,
// BindingFlags, MemberFilter,
// Object) 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(string);
        // Creating try-catch block for handling Exception
        try {
            // Declaring and initializing the object of MemberTypes
            // that indicates the type of member to search for.
            MemberTypes mt = MemberTypes.All;
            // Declaring and initializing the object of BindingFlags
            // that specify how the search is conducted.
            BindingFlags ba = BindingFlags.Public
                          | BindingFlags.Instance;
            // Declaring and initializing MemberFilter
            // which help the delegate that compares
            // the MemberInfo against filterCriteria.
            MemberFilter mf = new MemberFilter(Search);
            // Declaring and initializing object of filterCriteria
            // the search criteria that determines whether a member is
            // returned in the array of MemberInfo objects or not
            object filterCriteria = "Equals";
            // Getting filterd array of MemberInfo by
            // using FindMembers() Method
            MemberInfo[] info = objType.FindMembers(mt, ba, mf, filterCriteria);
            // Display the Result
            for (int index = 0; index < info.Length; index++)
                Console.WriteLine("Result of FindMembers -  {0}",
                                        info[index].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 Search(MemberInfo info, Object obj)
    {
        // Compare the name of the member
        // function with the filter criteria.
        if (info.Name.ToString() == obj.ToString())
            return true;
        else
            return false;
    }
}
输出:
Result of FindMembers -  Boolean Equals(System.Object)
Result of FindMembers -  Boolean Equals(System.String)
Result of FindMembers -  Boolean Equals(System.String, System.StringComparison)

参考:


相关用法


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