當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。