Partager via


Type.FindMembers Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Overridable Function FindMembers ( _
    memberType As MemberTypes, _
    bindingAttr As BindingFlags, _
    filter As MemberFilter, _
    filterCriteria As Object _
) As MemberInfo()
public virtual MemberInfo[] FindMembers(
    MemberTypes memberType,
    BindingFlags bindingAttr,
    MemberFilter filter,
    Object filterCriteria
)

Parameters

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

Exceptions

Exception Condition
ArgumentNullException

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

Remarks

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.StaticOrBindingFlags.NonPublic in Visual Basic).

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.

Examples

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

Imports System.Reflection

Class Example

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        Dim obj As Object = "a string"

        ' Get the type of the object, and find all the public static or 
        ' instance methods that match the filter criteria.
        Dim members() As MemberInfo = _
            obj.GetType().FindMembers(MemberTypes.Method, _
                BindingFlags.Public Or BindingFlags.Static Or BindingFlags.Instance, _
                New MemberFilter(AddressOf PartialName), _
                "ar")

        For Each mi As MemberInfo In Members
            outputBlock.Text &= "Found: " & mi.ToString() & vbLf
        Next 

    End Sub 

    Shared Function PartialName(ByVal candidate As MemberInfo, _
                                ByVal part As Object) As Boolean
        ' Test whether the name of the candidate member contains the 
        ' specified partial name.
        If candidate.Name.IndexOf(part.ToString()) > -1 Then
            Return True
        Else
            Return False
        End If
    End Function 
End Class 

' This example produces the following output:
'
'Found: Char get_Chars(Int32)
'Found: Char[] ToCharArray()
'Found: System.String TrimStart(Char[])
'Found: Int32 Compare(System.String, System.String)
'Found: Int32 Compare(System.String, System.String, System.StringComparison)
'Found: Int32 Compare(System.String, System.String, System.Globalization.CultureInfo, System.Globalization.CompareOptions)
'Found: Int32 Compare(System.String, Int32, System.String, Int32, Int32)
'Found: Int32 Compare(System.String, Int32, System.String, Int32, Int32, System.Globalization.CultureInfo, System.Globalization.CompareOptions)
'Found: Int32 Compare(System.String, Int32, System.String, Int32, Int32, System.StringComparison)
'Found: Int32 CompareTo(System.Object)
'Found: Int32 CompareTo(System.String)
'Found: Int32 CompareOrdinal(System.String, System.String)
'Found: Int32 CompareOrdinal(System.String, Int32, System.String, Int32, Int32)
'Found: Boolean StartsWith(System.String)
'Found: Boolean StartsWith(System.String, System.StringComparison)
using System;
using System.Reflection;

class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        Object obj = "a string";

        // Get the type of the object, and find all the public static or 
        // instance methods that match the filter criteria.
        MemberInfo[] members =
            obj.GetType().FindMembers(MemberTypes.Method, 
                BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance,
                new MemberFilter(PartialName), 
                "ar");

        foreach (MemberInfo mi in members)
        {
            outputBlock.Text += "Found: " + mi.ToString() + "\n";
        }
    }

    public static bool PartialName(MemberInfo candidate, Object part)
    {
        // Test whether the name of the candidate member contains the 
        // specified partial name.
        if (candidate.Name.IndexOf(part.ToString()) > -1) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

/* This example produces the following output:

Found: Char get_Chars(Int32)
Found: Char[] ToCharArray()
Found: System.String TrimStart(Char[])
Found: Int32 Compare(System.String, System.String)
Found: Int32 Compare(System.String, System.String, System.StringComparison)
Found: Int32 Compare(System.String, System.String, System.Globalization.CultureInfo, System.Globalization.CompareOptions)
Found: Int32 Compare(System.String, Int32, System.String, Int32, Int32)
Found: Int32 Compare(System.String, Int32, System.String, Int32, Int32, System.Globalization.CultureInfo, System.Globalization.CompareOptions)
Found: Int32 Compare(System.String, Int32, System.String, Int32, Int32, System.StringComparison)
Found: Int32 CompareTo(System.Object)
Found: Int32 CompareTo(System.String)
Found: Int32 CompareOrdinal(System.String, System.String)
Found: Int32 CompareOrdinal(System.String, Int32, System.String, Int32, Int32)
Found: Boolean StartsWith(System.String)
Found: Boolean StartsWith(System.String, System.StringComparison)
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.