Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get the specified members of the current Type in C#?
To get the specified members of the current Type in C#, we use the GetMember() method from the System.Reflection namespace. This method allows us to retrieve information about specific members of a type, such as fields, properties, methods, and events.
Syntax
Following is the syntax for getting specified members of a Type −
Type.GetMember(string name) Type.GetMember(string name, BindingFlags bindingAttr) Type.GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
Parameters
-
name − The string containing the name of the member to get.
-
bindingAttr − A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
-
type − The value to search for (fields, properties, methods, etc.).
Return Value
Returns an array of MemberInfo objects representing the specified members, or an empty array if no members are found.
Using GetMember() with Default Binding
Example
using System;
using System.Reflection;
public class Demo {
public static void Main() {
Type type = typeof(Subject);
try {
FieldInfo fieldInfo = type.GetField("SubName");
MemberInfo[] info = type.GetMember("SubName");
Console.Write("Members = ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
Console.WriteLine("FieldInfo = {0}", fieldInfo);
}
catch (ArgumentNullException e) {
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Subject {
public string SubName = "Science";
}
The output of the above code is −
Members = System.String SubName FieldInfo = System.String SubName
Using GetMember() with BindingFlags
Example
using System;
using System.Reflection;
public class Demo {
public static void Main() {
Type type = typeof(Subject);
try {
FieldInfo fieldInfo = type.GetField("SubName");
MemberInfo[] info = type.GetMember("SubName", BindingFlags.Public | BindingFlags.Instance);
Console.Write("Members = ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
Console.WriteLine("FieldInfo = {0}", fieldInfo);
}
catch (ArgumentNullException e) {
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Subject {
public string SubName = "Science";
}
The output of the above code is −
Members = System.String SubName FieldInfo = System.String SubName
Getting Multiple Member Types
Example
using System;
using System.Reflection;
public class Demo {
public static void Main() {
Type type = typeof(Student);
MemberInfo[] allMembers = type.GetMembers(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("All public instance members:");
foreach (MemberInfo member in allMembers) {
Console.WriteLine(" {0} - {1}", member.Name, member.MemberType);
}
Console.WriteLine("\nSpecific member 'Name':");
MemberInfo[] nameMembers = type.GetMember("Name");
foreach (MemberInfo member in nameMembers) {
Console.WriteLine(" {0} - {1}", member.Name, member.MemberType);
}
}
}
public class Student {
public string Name = "John";
public int Age { get; set; } = 20;
public void Study() { }
}
The output of the above code is −
All public instance members: get_Age - Method set_Age - Method Study - Method GetType - Method ToString - Method Equals - Method GetHashCode - Method Name - Field Age - Property Specific member 'Name': Name - Field
Common BindingFlags
| BindingFlag | Description |
|---|---|
| Public | Includes public members in the search |
| NonPublic | Includes non-public members in the search |
| Instance | Includes instance members in the search |
| Static | Includes static members in the search |
Conclusion
The GetMember() method in C# provides a powerful way to retrieve specific members of a Type using reflection. It returns an array of MemberInfo objects that can be used to examine and work with the members dynamically at runtime.
