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

Definition

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

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

Parameters

memberType
MemberTypes

A bitwise combination of the enumeration values that indicates the type of member to search for.

bindingAttr
BindingFlags

A bitwise combination of the enumeration values that specify how the search is conducted.

-or-

Default to return null.

filter
MemberFilter

The delegate that does the comparisons, returning true if the member currently being inspected matches the filterCriteria and false otherwise.

filterCriteria
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.

Returns

A filtered array of MemberInfo objects of the specified member type.

-or-

An empty array if the current Type does not have members of type memberType that match the filter criteria.

Implements

Exceptions

filter is null.

Examples

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

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)
*/

Remarks

This method can be overridden by a derived class.

Members include properties, methods, fields, events, and so on.

For 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.

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

  • 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, internal, 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.

To get the class initializer (static constructor) using this method, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.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 filter argument can be a custom delegate of type MemberFilter, or it can be one of the following predefined delegates:

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also