Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Type.FindMembers Method

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public virtual MemberInfo[] FindMembers (
	MemberTypes memberType,
	BindingFlags bindingAttr,
	MemberFilter filter,
	Object filterCriteria
)
public MemberInfo[] FindMembers (
	MemberTypes memberType, 
	BindingFlags bindingAttr, 
	MemberFilter filter, 
	Object filterCriteria
)
public function FindMembers (
	memberType : MemberTypes, 
	bindingAttr : BindingFlags, 
	filter : MemberFilter, 
	filterCriteria : Object
) : MemberInfo[]

Parameters

memberType

A MemberTypes object indicating the type of member to search for.

bindingAttr

A bitmask comprised of one or more BindingFlags that specify how the search is conducted.

-or-

Zero, to return a null reference (Nothing in Visual Basic).

filter

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

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

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.

Exception typeCondition

ArgumentNullException

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

This method can be overridden by a derived class.

Members include properties, methods, fields, events, and so on. If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).

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

  • Instance to include instance members in the search.

  • Static to include static members in the search.

  • Public to include public members in the search.

  • 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:

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

import System.*;
import System.Reflection.*;

class MyFindMembersClass
{
    public static void main(String[] args)
    {
        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.get_Item(index).ToString() + "\n");
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception : " + e.ToString());
        }
    } //main

    public static boolean DelegateToSearchCriteria(MemberInfo objMemberInfo,
        Object objSearch)
    {
        // Compare the name of the member function with the filter criteria.
        if (objMemberInfo.get_Name().ToString().Equals(objSearch.ToString())) {
            return true;
        }
        else {
            return false;
        }
    } //DelegateToSearchCriteria
} //MyFindMembersClass

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

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

Show:
© 2014 Microsoft