共用方式為


Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) 方法

定義

傳回指定成員類型的 MemberInfo 物件的篩選陣列。

public:
 virtual cli::array <System::Reflection::MemberInfo ^> ^ FindMembers(System::Reflection::MemberTypes memberType, System::Reflection::BindingFlags bindingAttr, System::Reflection::MemberFilter ^ filter, System::Object ^ filterCriteria);
public virtual System.Reflection.MemberInfo[] FindMembers (System.Reflection.MemberTypes memberType, System.Reflection.BindingFlags bindingAttr, System.Reflection.MemberFilter? filter, object? filterCriteria);
public virtual System.Reflection.MemberInfo[] FindMembers (System.Reflection.MemberTypes memberType, System.Reflection.BindingFlags bindingAttr, System.Reflection.MemberFilter filter, object filterCriteria);
abstract member FindMembers : System.Reflection.MemberTypes * System.Reflection.BindingFlags * System.Reflection.MemberFilter * obj -> System.Reflection.MemberInfo[]
override this.FindMembers : System.Reflection.MemberTypes * System.Reflection.BindingFlags * System.Reflection.MemberFilter * obj -> System.Reflection.MemberInfo[]
Public Overridable Function FindMembers (memberType As MemberTypes, bindingAttr As BindingFlags, filter As MemberFilter, filterCriteria As Object) As MemberInfo()

參數

memberType
MemberTypes

列舉值的位元組合,表示要搜尋的成員型別。

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回 nullDefault

filter
MemberFilter

執行比較的委派,如果目前進行檢查的成員符合 true,則傳回 filterCriteria,否則傳回 false

filterCriteria
Object

判斷在 MemberInfo 物件的陣列中是否傳回成員的搜尋準則。

FieldAttributesMethodAttributesMethodImplAttributes 的欄位可以與這個類別所提供的 FilterAttribute 委派結合使用。

傳回

指定之成員類型的 MemberInfo 物件篩選陣列。

-或-

如果目前 Type 沒有符合篩選準則的 memberType 型別成員,則為空陣列。

實作

例外狀況

filternull

範例

下列範例會尋找類別中符合指定搜尋準則的所有成員,然後顯示相符的成員。

using namespace System;
using namespace System::Reflection;
ref class MyFindMembersClass
{
public:
   static void Test()
   {
      Object^ objTest = gcnew Object;
      Type^ objType = objTest->GetType();
      array<MemberInfo^>^arrayMemberInfo;
      try
      {
         
         //Find all static or public methods in the Object class that match the specified name.
         arrayMemberInfo = objType->FindMembers( MemberTypes::Method, static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static | BindingFlags::Instance), gcnew MemberFilter( DelegateToSearchCriteria ), "ReferenceEquals" );
         for ( int index = 0; index < arrayMemberInfo->Length; index++ )
            Console::WriteLine( "Result of FindMembers -\t {0}", String::Concat( arrayMemberInfo[ index ], "\n" ) );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Exception : {0}", e );
      }

   }

   static bool DelegateToSearchCriteria( MemberInfo^ objMemberInfo, Object^ objSearch )
   {
      
      // Compare the name of the member function with the filter criteria.
      if ( objMemberInfo->Name->Equals( objSearch->ToString() ) )
            return true;
      else
            return false;
   }

};

int main()
{
   MyFindMembersClass::Test();
}
/* The example produces the following output:

Result of FindMembers - Boolean ReferenceEquals(System.Object, System.Object)
*/
using System;
using System.Reflection;

class MyFindMembersClass
{
    public static void Main()
    {
        Object objTest = new Object();
        Type objType = objTest.GetType ();
        MemberInfo[] arrayMemberInfo;
        try
        {
            //Find all static or public methods in the Object class that match the specified name.
            arrayMemberInfo = objType.FindMembers(MemberTypes.Method,
                BindingFlags.Public | BindingFlags.Static| BindingFlags.Instance,
                new MemberFilter(DelegateToSearchCriteria),
                "ReferenceEquals");

            for(int index=0;index < arrayMemberInfo.Length ;index++)
                Console.WriteLine ("Result of FindMembers -\t"+ arrayMemberInfo[index].ToString() +"\n");
        }
        catch (Exception e)
        {
            Console.WriteLine ("Exception : " + e.ToString() );
        }
    }
    public static bool DelegateToSearchCriteria(MemberInfo objMemberInfo, Object objSearch)
    {
        // Compare the name of the member function with the filter criteria.
        if(objMemberInfo.Name.ToString() == objSearch.ToString())
            return true;
        else
            return false;
    }
}
/* The example produces the following output:

Result of FindMembers - Boolean ReferenceEquals(System.Object, System.Object)
*/
open System.Reflection

let delegateToSearchCriteria (objMemberInfo: MemberInfo) (objSearch: obj) =
    // Compare the name of the member function with the filter criteria.
    string objMemberInfo.Name = string objSearch

let objTest = obj ()
let objType = objTest.GetType ()
try
    //Find all static or public methods in the Object class that match the specified name.
    let arrayMemberInfo = 
        objType.FindMembers(MemberTypes.Method, BindingFlags.Public ||| BindingFlags.Static ||| BindingFlags.Instance, MemberFilter delegateToSearchCriteria, "ReferenceEquals")

    for info in arrayMemberInfo do
        printfn $"Result of FindMembers -\t{info}\n"
with e ->
    printfn $"Exception : {e}"

(* The example produces the following output:

Result of FindMembers - Boolean ReferenceEquals(System.Object, System.Object)
*)

Imports System.Reflection

Class MyFindMembersClass

    Public Shared Sub Main()
        Dim objTest As New Object()
        Dim objType As Type = objTest.GetType()
        Dim arrayMemberInfo() As MemberInfo
        Try
            'Find all static or public methods in the Object 
            'class that match the specified name.
            arrayMemberInfo = objType.FindMembers(MemberTypes.Method, _
                              BindingFlags.Public Or BindingFlags.Static _
                              Or BindingFlags.Instance, _
                              New MemberFilter(AddressOf DelegateToSearchCriteria), _
                              "ReferenceEquals")

            Dim index As Integer
            For index = 0 To arrayMemberInfo.Length - 1
                Console.WriteLine("Result of FindMembers -" + ControlChars.Tab + _
                               arrayMemberInfo(index).ToString() + ControlChars.Cr)
            Next index
        Catch e As Exception
            Console.WriteLine("Exception : " + e.ToString())
        End Try
    End Sub

    Public Shared Function DelegateToSearchCriteria _
                            (ByVal objMemberInfo As MemberInfo, _
                             ByVal objSearch As Object) As Boolean
        ' Compare the name of the member function with the filter criteria.
        If objMemberInfo.Name.ToString() = objSearch.ToString() Then
            Return True
        Else
            Return False
        End If
    End Function 'DelegateToSearchCriteria 
End Class
' The example produces the following output:
'
' Result of FindMembers - Boolean ReferenceEquals(System.Object, System.Object)

備註

這個方法可由衍生類別覆寫。

成員包括屬性、方法、欄位、事件等等。

FindMembers若要讓 方法成功擷取成員資訊, bindingAttr 引數必須包含至少一個 BindingFlags.InstanceBindingFlags.Static ,以及至少一個 BindingFlags.NonPublicBindingFlags.Public

下列 BindingFlags 篩選旗標可用來定義要包含在搜尋中的成員:

  • 指定 BindingFlags.Instance 以在搜尋中包含實例成員。

  • 指定 BindingFlags.Static 以在搜尋中包含靜態成員。

  • 指定 BindingFlags.Public 以在搜尋中包含公用成員。

  • 指定 BindingFlags.NonPublic 在搜尋中包含非公用成員, (為私用、內部和受保護的) 成員。

下列 BindingFlags 修飾詞旗標可用來變更搜尋的運作方式:

  • BindingFlags.DeclaredOnly 只搜尋 在 上 Type 宣告的成員,而不是只繼承的成員。

如需相關資訊,請參閱 System.Reflection.BindingFlags

若要使用此方法取得靜態建構函式 (類別初始化運算式) ,您必須在 Visual Basic) 中指定 BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic 。 您也可以使用 TypeInitializer 屬性取得類別初始化運算式。

如果目前的 Type 代表泛型型別或泛型方法的類型參數, FindMembers 則會處理類別條件約束所宣告的任何成員,以及型別參數的介面條件約束。

filter 變數可以是 類型的 MemberFilter 自訂委派,也可以是下列其中一個預先定義的委派:

適用於

另請參閱