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- 若为 Default,则返回 nullDefault 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 'Main

    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 'MyFindMembersClass

注解

此方法可由派生类重写。This method can be overridden by a derived class.

成员包括属性、方法、字段、事件等。Members include properties, methods, fields, events, and so on.

BindingFlags.NonPublic BindingFlags.Instance BindingFlags.Static BindingFlags.Public要使bindingAttr方法成功检索成员信息,参数必须包含至少一个和中的至少一个和和。 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.Static必须BindingFlags.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:

适用于

另请参阅