Type.Findmembers() Method In C#

In the world of C# development, the Type class acts as a powerful entity, providing a way to examine and interact with the metadata of types contained in an assembly. Deep within this class lies a powerful function called FindMembers. This function gives developers the ability to search for members like functions, attributes, and variables within a particular type according to criteria defined by the user. This article seeks to break down the structure, usage, and provide a hands-on illustration with results to demonstrate the capabilities of Type.FindMembers.

Syntax:

It has the following syntax:

Example

public MemberInfo[] FindMembers(
 MemberTypes memberType,
 BindingFlags bindingAttr,
 MemberFilter filter,
 object filterCriteria
);
  • memberType: It specifies the sought-after type of member (e.g., method, property, field).
  • bindingAttr: It dictates the binding attributes for the search (e.g., public, non-public, static).
  • filter: It is a delegate that fashions the criteria against which members are measured.
  • filterCriteria: The touchstone criteria employed for member filtration.
  • Example:

Let's consider a scenario to demonstrate the implementation of the Type.FindMembers method in C#.

Example

using System;
using System.Reflection;
 
class Program
{
 static void Main()
 {
 // Uncover the essence of the String class
 Type stringType = typeof(string);
 
 // Craft a bespoke filter to unveil public instance methods
 MemberFilter filter = (member, criteria) =>
 member.MemberType == MemberTypes.Method &&
 member.DeclaringType == typeof(string) &&
 (member as MethodInfo)?.IsPublic == true &&
 (member as MethodInfo)?.IsStatic == false;
 
 // Employ Type.FindMembers() to disclose public instance methods of the String class
 MemberInfo[] members = stringType.FindMembers(
 MemberTypes.Method,
 BindingFlags.Public | BindingFlags.Instance,
 filter,
 null
 );
 
 // Showcase the unearthed members
 Console.WriteLine("Public instance methods of String class:");
 foreach (var member in members)
 {
 Console.WriteLine(member.Name);
 }
 }
}

Output:

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
  • Discovery of Type: The script commences by uncovering details about the String class through the utilization of the Type class from the Reflection namespace. The line Type stringType = typeof(string); acquires the type information pertaining to the String
  • Creation of a Customized Filter: A distinctive filter, denoted as filter , is meticulously fashioned using the MemberFilter This filter is specifically designed to reveal public instance methods within the confines of the String
  • Filter Criteria: The filter assesses multiple conditions for each member: MemberType == MemberTypes.Method: Guarantees that the member corresponds to a method. DeclaringType == typeof(string): Validates the membership of the member within the String class. (member as MethodInfo)?.IsPublic == true: Verifies the public accessibility of the method. (member as MethodInfo)?.IsStatic == false: Ensures the method is non-static. Utilization of Type.FindMembers: The method FindMembers is invoked to conduct a targeted search for members based on the specified criteria. Parameters encompass the type of member ( Method ) , binding attributes ( BindingFlags.Public | BindingFlags.Instance ) , the custom filter ( filter ) , and criteria ( null in this instance). Array of MemberInfo: The outcome of the operation involving FindMembers is an array comprising MemberInfo objects signifying the members that align with the applied filters. Presentation of Results: The script proceeds to exhibit the unearthed members by traversing the array of MemberInfo . The name of each member is presented through WriteLine(member.Name) . Result Output: The program's output manifests as a compilation of names representing public instance methods within the String class that conform to the predefined conditions.
  • The script commences by uncovering details about the String class through the utilization of the Type class from the Reflection namespace.
  • The line Type stringType = typeof(string); acquires the type information pertaining to the String
  • A distinctive filter, denoted as filter , is meticulously fashioned using the MemberFilter
  • This filter is specifically designed to reveal public instance methods within the confines of the String
  • The filter assesses multiple conditions for each member: MemberType == MemberTypes.Method: Guarantees that the member corresponds to a method. DeclaringType == typeof(string): Validates the membership of the member within the String class. (member as MethodInfo)?.IsPublic == true: Verifies the public accessibility of the method. (member as MethodInfo)?.IsStatic == false: Ensures the method is non-static.
  • Utilization of Type.FindMembers: The method FindMembers is invoked to conduct a targeted search for members based on the specified criteria. Parameters encompass the type of member ( Method ) , binding attributes ( BindingFlags.Public | BindingFlags.Instance ) , the custom filter ( filter ) , and criteria ( null in this instance).
  • Array of MemberInfo: The outcome of the operation involving FindMembers is an array comprising MemberInfo objects signifying the members that align with the applied filters.
  • Presentation of Results: The script proceeds to exhibit the unearthed members by traversing the array of MemberInfo . The name of each member is presented through WriteLine(member.Name) .
  • Result Output: The program's output manifests as a compilation of names representing public instance methods within the String class that conform to the predefined conditions.
  • MemberType == MemberTypes.Method: Guarantees that the member corresponds to a method.
  • DeclaringType == typeof(string): Validates the membership of the member within the String class.
  • (member as MethodInfo)?.IsPublic == true: Verifies the public accessibility of the method.
  • (member as MethodInfo)?.IsStatic == false: Ensures the method is non-static.
  • The method FindMembers is invoked to conduct a targeted search for members based on the specified criteria.
  • Parameters encompass the type of member ( Method ) , binding attributes ( BindingFlags.Public | BindingFlags.Instance ) , the custom filter ( filter ) , and criteria ( null in this instance).
  • The outcome of the operation involving FindMembers is an array comprising MemberInfo objects signifying the members that align with the applied filters.
  • The script proceeds to exhibit the unearthed members by traversing the array of MemberInfo .
  • The name of each member is presented through WriteLine(member.Name) .
  • The program's output manifests as a compilation of names representing public instance methods within the String class that conform to the predefined conditions.
  • Conclusion:

In essence, the highlighted C# code demonstrates the strong capabilities embedded within the Type.FindMembers method, showcasing its versatile nature for dynamically exploring and filtering class members during runtime. The first task entails recognizing the String class type using the Type class. A carefully designed filter, making use of the MemberFilter delegate, enables the retrieval of public instance methods according to specified conditions such as method type, declaring class, and accessibility.

Employing these conditions strategically guarantees a precise and polished outcome, showcasing the effectiveness of the Type.FindMembers function in dynamic member exploration. By utilizing this function effectively, programmers acquire a functional approach to engaging with class configurations dynamically. The collection of MemberInfo objects that results acts as a concrete depiction of the selected members. This script, designed for tasks involving reflection, offers a detailed view of the complexities of locating methods within a designated class. Ultimately, the Type.FindMembers function proves to be a versatile and essential instrument for analyzing runtime details, illuminating the intricate metadata of class arrangements in the C# programming domain.

Input Required

This code uses input(). Please provide values below: