Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) Method is used to return a filtered array of MemberInfo objects of the specified member type.
Syntax:
public virtual System.Reflection.MemberInfo[] FindMembers (System.Reflection.MemberTypes memberType, System.Reflection.BindingFlags bindingAttr, System.Reflection.MemberFilter filter, object filterCriteria);
Parameters:
- memberType: It indicates that what type of member should be searched.
- bindingAttr: It used to specify how the search is conducted or Zero, to return null.
- filter: It does the comparisons, returning true if the member currently being inspected matches the filterCriteria and false otherwise.
- filterCriteria: The search criteria that determines whether a member is returned in the array of MemberInfo objects.
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, internal, and protected members) in the search.
Return Value: This method returns A filtered array of MemberInfo objects of the specified member type. or An empty array of type MemberInfo,
Exception: This method throws ArgumentNullException if filter is null.
Example:
// C# program to demonstrate the // Type.FindMembers(MemberTypes, // BindingFlags, MemberFilter, // Object) Method using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof ( string );
// Creating try-catch block for handling Exception
try {
// Declaring and initializing the object of MemberTypes
// that indicates the type of member to search for.
MemberTypes mt = MemberTypes.All;
// Declaring and initializing the object of BindingFlags
// that specify how the search is conducted.
BindingFlags ba = BindingFlags.Public
| BindingFlags.Instance;
// Declaring and initializing MemberFilter
// which help the delegate that compares
// the MemberInfo against filterCriteria.
MemberFilter mf = new MemberFilter(Search);
// Declaring and initializing object of filterCriteria
// the search criteria that determines whether a member is
// returned in the array of MemberInfo objects or not
object filterCriteria = "Equals" ;
// Getting filtered array of MemberInfo by
// using FindMembers() Method
MemberInfo[] info = objType.FindMembers(mt, ba, mf, filterCriteria);
// Display the Result
for ( int index = 0; index < info.Length; index++)
Console.WriteLine( "Result of FindMembers - {0}" ,
info[index].ToString());
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
// Defining MyInterfaceFilter
// which helps to fix the certain condition
// on which filtration took place
public static bool Search(MemberInfo info, Object obj)
{
// Compare the name of the member
// function with the filter criteria.
if (info.Name.ToString() == obj.ToString())
return true ;
else
return false ;
}
} |
Result of FindMembers - Boolean Equals(System.Object) Result of FindMembers - Boolean Equals(System.String) Result of FindMembers - Boolean Equals(System.String, System.StringComparison)
Reference: