Advanced C# Reflection: Using `Type.FindMembers()` for Targeted Member Retrieval

Master advanced reflection techniques in C# using `Type.FindMembers()`. This tutorial demonstrates how to search for specific type members (methods, properties, fields) at runtime based on customizable criteria using `MemberTypes`, `BindingFlags`, and a `MemberFilter`. Build dynamic and adaptable C# applications.



Using the `Type.FindMembers()` Method in C# for Reflection

Introduction

The `Type.FindMembers()` method in C# is a powerful reflection technique that lets you search for specific members (methods, properties, fields, etc.) of a type at runtime based on customizable criteria. This is useful for inspecting and manipulating types dynamically.

`Type.FindMembers()` Syntax and Parameters

`Type.FindMembers()` Syntax

public MemberInfo[] FindMembers(
    MemberTypes memberType, 
    BindingFlags bindingAttr, 
    MemberFilter filter, 
    object filterCriteria
);
  • memberType: Specifies the type of member to search for (e.g., `MemberTypes.Method`, `MemberTypes.Property`, `MemberTypes.Field`).
  • bindingAttr: `BindingFlags` that define the scope of the search (e.g., `BindingFlags.Public`, `BindingFlags.Instance`, `BindingFlags.Static`).
  • filter: A `MemberFilter` delegate that allows for custom filtering logic. This delegate takes a `MemberInfo` and `object` as input and returns true if the member should be included in the results.
  • filterCriteria: An object passed to the `filter` delegate; you can use this to provide additional filtering criteria.

Return Value

An array of `MemberInfo` objects representing the members that match the specified criteria.

Example: Finding Public Instance Methods of the `String` Class

Finding Public Instance Methods of String

using System;
using System.Reflection;

class Program {
    static void Main(string[] args) {
        Type stringType = typeof(string);
        MemberFilter filter = (member, criteria) =>
            member.MemberType == MemberTypes.Method &&
            member.DeclaringType == typeof(string) &&
            (member as MethodInfo)?.IsPublic == true &&
            (member as MethodInfo)?.IsStatic == false;

        MemberInfo[] members = stringType.FindMembers(
            MemberTypes.Method,
            BindingFlags.Public | BindingFlags.Instance,
            filter,
            null
        );

        Console.WriteLine("Public instance methods of String class:");
        foreach (var member in members) {
            Console.WriteLine(member.Name);
        }
    }
}
Example Output

Public instance methods of String class:
Clone
CompareTo
EndsWith
Equals
GetEnumerator
GetHashCode
GetType
IndexOf
LastIndexOf
Normalize
PadLeft
PadRight
Remove
Replace
Split
StartsWith
Substring
ToCharArray
ToLower
ToLowerInvariant
ToString
ToUpper
ToUpperInvariant
Trim
TrimEnd
TrimStart
        

Conclusion

The `Type.FindMembers()` method is a powerful tool for runtime code introspection. By combining different `MemberTypes` and `BindingFlags` with a custom `MemberFilter`, you can precisely target the members you need, enabling dynamic code analysis and manipulation.