Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
Type Class
Type Methods
 FindMembers Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Type..::.FindMembers Method

Updated: September 2009

Returns a filtered array of MemberInfo objects of the specified member type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Overridable Function FindMembers ( _
    memberType As MemberTypes, _
    bindingAttr As BindingFlags, _
    filter As MemberFilter, _
    filterCriteria As Object _
) As MemberInfo()
Visual Basic (Usage)
Dim instance As Type
Dim memberType As MemberTypes
Dim bindingAttr As BindingFlags
Dim filter As MemberFilter
Dim filterCriteria As Object
Dim returnValue As MemberInfo()

returnValue = instance.FindMembers(memberType, _
    bindingAttr, filter, filterCriteria)
C#
public virtual MemberInfo[] FindMembers(
    MemberTypes memberType,
    BindingFlags bindingAttr,
    MemberFilter filter,
    Object filterCriteria
)
Visual C++
public:
virtual array<MemberInfo^>^ FindMembers(
    MemberTypes memberType, 
    BindingFlags bindingAttr, 
    MemberFilter^ filter, 
    Object^ filterCriteria
)
JScript
public function FindMembers(
    memberType : MemberTypes, 
    bindingAttr : BindingFlags, 
    filter : MemberFilter, 
    filterCriteria : Object
) : MemberInfo[]

Parameters

memberType
Type: System.Reflection..::.MemberTypes
A MemberTypes object indicating the type of member to search for.
bindingAttr
Type: System.Reflection..::.BindingFlags
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
-or-
Zero, to return nullNothingnullptra null reference (Nothing in Visual Basic).
filter
Type: System.Reflection..::.MemberFilter
The delegate that does the comparisons, returning true if the member currently being inspected matches the filterCriteria and false otherwise. You can use the FilterAttribute, FilterName, and FilterNameIgnoreCase delegates supplied by this class. The first uses the fields of FieldAttributes, MethodAttributes, and MethodImplAttributes as search criteria, and the other two delegates use String objects as the search criteria.
filterCriteria
Type: System..::.Object
The search criteria that determines whether a member is returned in the array of MemberInfo objects.
The fields of FieldAttributes, MethodAttributes, and MethodImplAttributes can be used in conjunction with the FilterAttribute delegate supplied by this class.

Return Value

Type: array<System.Reflection..::.MemberInfo>[]()[]
A filtered array of MemberInfo objects of the specified member type.
-or-
An empty array of type MemberInfo, if the current Type does not have members of type memberType that match the filter criteria.

Implements

_Type..::.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object)
ExceptionCondition
ArgumentNullException

filter is nullNothingnullptra null reference (Nothing in Visual Basic).

This method can be overridden by a derived class.

The following BindingFlags filter flags can be used to define which members to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • Specify BindingFlags.Instance to include instance members in the search.

  • Specify BindingFlags.Static to include static members in the search.

  • Specify BindingFlags.Public to include public members in the search.

  • Specify BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.

The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly to search only the members declared on the Type, not members that were simply inherited.

See System.Reflection..::.BindingFlags for more information.

Valid values for MemberType are defined in MemberInfo. If no such members are found, an empty array is returned.

To get the class initializer (.cctor) using this method, you must specify BindingFlags..::.Static | BindingFlags..::.NonPublic (BindingFlags..::.Static Or BindingFlags..::.NonPublic in Visual Basic). You can also get the class initializer using the TypeInitializer property.

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.

The following example finds all the members in a class that match the specified search criteria, and then displays the matched members.

Visual Basic
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

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
C#
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;
    }
}
Visual C++
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();
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Date

History

Reason

September 2009

Removed an erroneous statement that nullNothingnullptra null reference (Nothing in Visual Basic) is returned for non-public members outside the assembly, if caller lacks ReflectionPermission.

Content bug fix.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker