Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) Method

定義

傳回指定成員類型的 MemberInfo 物件的篩選陣列。Returns a filtered array of MemberInfo objects of the specified member type.

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);
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 MemberTypes MemberTypes MemberTypes

列舉值的位元組合,表示要搜尋的成員型別。A bitwise combination of the enumeration values that indicates the type of member to search for.

bindingAttr
BindingFlags BindingFlags BindingFlags BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or- 要傳回 nullDefaultDefault to return null.

filter
MemberFilter MemberFilter MemberFilter MemberFilter

執行比較的委派,如果目前進行檢查的成員符合 true,則傳回 filterCriteria,否則傳回 falseThe delegate that does the comparisons, returning true if the member currently being inspected matches the filterCriteria and false otherwise.

filterCriteria
Object Object Object Object

判斷在 MemberInfo 物件的陣列中是否傳回成員的搜尋準則。The search criteria that determines whether a member is returned in the array of MemberInfo objects.

FieldAttributesMethodAttributesMethodImplAttributes 的欄位可以與這個類別所提供的 FilterAttribute 委派結合使用。The fields of FieldAttributes, MethodAttributes, and MethodImplAttributes can be used in conjunction with the FilterAttribute delegate supplied by this class.

傳回

指定之成員類型的 MemberInfo 物件篩選陣列。A filtered array of MemberInfo objects of the specified member type.

-或--or- 如果目前 Type 沒有符合篩選準則的 memberType 型別成員,則為空陣列。An empty array if the current Type does not have members of type memberType that match the filter criteria.

實作

例外狀況

範例

下列範例會尋找符合指定搜尋準則之類別中的所有成員,然後顯示相符的成員。The following example finds all the members in a class that match the specified search criteria, and then displays the matched members.

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();
}

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;
    }
}

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

備註

衍生類別可以覆寫這個方法。This method can be overridden by a derived class.

成員包括屬性、方法、欄位、事件等。Members include properties, methods, fields, events, and so on.

BindingFlags.NonPublic BindingFlags.Instance bindingAttr BindingFlags.Public若要讓BindingFlags.Static方法成功取得成員資訊,引數必須包含至少一個和,以及至少一個和。 FindMembersFor the FindMembers method to successfully retrieve member information, the bindingAttr argument must include at least one of BindingFlags.Instance and BindingFlags.Static, along with at least one of BindingFlags.NonPublic and BindingFlags.Public.

下列BindingFlags篩選旗標可以用來定義要包含在搜尋中的成員:The following BindingFlags filter flags can be used to define which members to include in the search:

  • 指定BindingFlags.Instance要在搜尋中包含實例成員。Specify BindingFlags.Instance to include instance members in the search.

  • 指定BindingFlags.Static要在搜尋中包含靜態成員。Specify BindingFlags.Static to include static members in the search.

  • 指定BindingFlags.Public要在搜尋中包含公用成員。Specify BindingFlags.Public to include public members in the search.

  • 指定BindingFlags.NonPublic在搜尋中包含非公用成員(也就是私用、內部和受保護的成員)。Specify BindingFlags.NonPublic to include non-public members (that is, private, internal, and protected members) in the search.

下列BindingFlags的修飾詞旗標可以用來變更搜尋的運作方式:The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly表示只搜尋在上Type宣告的成員,而不是直接繼承的成員。BindingFlags.DeclaredOnly to search only the members declared on the Type, not members that were simply inherited.

如需詳細資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

若要使用這個方法取得類別初始化運算式(靜態的函式), BindingFlags.StaticBindingFlags.Static必須指定| BindingFlags.NonPublicOr BindingFlags.NonPublic在 Visual Basic 中)。To get the class initializer (static constructor) using this method, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic in Visual Basic). 您也可以使用TypeInitializer屬性來取得類別初始化運算式。You can also get the class initializer using the TypeInitializer property.

如果目前Type的表示泛型型別或泛型方法的類型參數, FindMembers則會處理由類別條件約束所宣告的任何成員,以及類型參數的介面條件約束。If the current Type represents a type parameter of a generic type or generic method, FindMembers processes any members declared by the class constraint and the interface constraints of the type parameter.

引數可以是類型MemberFilter的自訂委派,也可以是下列其中一個預先定義的委派: filterThe filter argument can be a custom delegate of type MemberFilter, or it can be one of the following predefined delegates:

適用於

另請參閱