Javatpoint Logo
Javatpoint Logo

Type.FindMembers() Method in C#

In the realm of C# programming, the Type class stands as a formidable entity, offering a means to scrutinize and engage with the metadata of types within an assembly. Nestled within this class is a potent method known as FindMembers(). This method empowers developers to hunt for members, such as methods, properties, and fields, within a specified type based on user-defined criteria. This blog post aims to dissect the syntax, application, and furnish a practical example with output to showcase the prowess of Type.FindMembers().

Syntax:

It has the following syntax:

  • 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 us take an example to illustrate the Type.FindMembers() method in C#.

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.

Conclusion:

In summary, the showcased C# code underscores the robust functionality inherent in the Type.FindMembers() method, illustrating its dynamic potential for runtime exploration and selective filtering of class members. The initial step involves identifying the type of the String class through the utilization of the Type class. A meticulously crafted custom filter, leveraging the MemberFilter delegate, facilitates the extraction of public instance methods based on predefined criteria, including method type, declaring class, and accessibility.

The strategic application of these conditions ensures a focused and refined result set, exemplifying the power of the Type.FindMembers() method in dynamic member discovery. By efficiently employing this method, developers gain a practical means to interact with class structures dynamically. The resulting array of MemberInfo objects serves as a tangible representation of the filtered members. This code, tailored for reflection-based tasks, provides a nuanced perspective on the intricacies of method discovery within a specified class. Ultimately, the Type.FindMembers() method emerges as an adaptable and indispensable tool for runtime introspection, shedding light on the nuanced metadata of class structures in the C# programming language.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA