C# Type FindMembers

Description

Type FindMembers returns a filtered array of MemberInfo objects of the specified member type.

Syntax

Type.FindMembers has the following syntax.


public virtual MemberInfo[] FindMembers(
  MemberTypes memberType,/*  www. j  a  v  a  2s .com*/
  BindingFlags bindingAttr,
  MemberFilter filter,
  Object filterCriteria
)

Parameters

Type.FindMembers has the following parameters.

  • memberType - An object that indicates the type of member to search for.
  • bindingAttr - A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
  • bindingAttr - -or-
  • bindingAttr - Zero, to return null.
  • 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.
  • filterCriteria - The fields of FieldAttributes, MethodAttributes, and MethodImplAttributes can be used in conjunction with the FilterAttribute delegate supplied by this class.

Returns

Type.FindMembers method returns

Example

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


using System;//w w  w .j a v  a 2 s.c om
using System.Reflection;

class MyFindMembersClass
{
    public static void Main()
    {
        Object objTest = new Object();
        Type objType = objTest.GetType ();
        MemberInfo[] arrayMemberInfo;
   
        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");                 
        }
    }
    public static bool DelegateToSearchCriteria(MemberInfo objMemberInfo, Object objSearch)
    {
        if(objMemberInfo.Name.ToString() == objSearch.ToString()){
            return true;
        }else {
            return false;
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version