Type.GetMember() Method in C#

The Type.GetMember() method in C# is used to retrieve information about specified members of the current Type using reflection. It returns an array of MemberInfo objects that represent the members matching the specified name and binding criteria.

Syntax

Following are the syntax variations for the Type.GetMember() method −

public System.Reflection.MemberInfo[] GetMember(string name);
public virtual System.Reflection.MemberInfo[] GetMember(string name, System.Reflection.BindingFlags bindingAttr);

Parameters

  • name − A string specifying the name of the member to search for.

  • bindingAttr − A combination of BindingFlags values that control how the search is conducted.

Return Value

Returns an array of MemberInfo objects representing the public members with the specified name, or an empty array if no members are found.

Type.GetMember() Process Type Object typeof(Class) MemberInfo[] Array of members matching the name GetMember("name") Fields, Properties, Methods, Events, etc. All member types can be retrieved

Using GetMember() with Member Name

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

Retrieving Multiple Member Types

Example

using System;
using System.Reflection;

public class Employee {
    public string Name = "John";
    public string Name { get; set; } = "Jane";
    
    public void DisplayInfo() {
        Console.WriteLine("Employee Info");
    }
}

public class Demo {
    public static void Main() {
        Type type = typeof(Employee);
        MemberInfo[] members = type.GetMembers(BindingFlags.Public | BindingFlags.Instance);
        
        Console.WriteLine("All public instance members:");
        foreach (MemberInfo member in members) {
            Console.WriteLine("{0} - {1}", member.Name, member.MemberType);
        }
    }
}

The output of the above code is −

All public instance members:
get_Name - Method
set_Name - Method
DisplayInfo - Method
GetType - Method
ToString - Method
Equals - Method
GetHashCode - Method
Name - Property
.ctor - Constructor

Common BindingFlags Values

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
DeclaredOnly Searches only the members declared on the Type

Conclusion

The Type.GetMember() method is a powerful reflection tool for retrieving member information from types at runtime. It supports flexible searching using BindingFlags and returns detailed MemberInfo objects that describe fields, properties, methods, and other class members.

Updated on: 2026-03-17T07:04:35+05:30

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements