Get the members of the current Type in C#

In C#, you can use reflection to inspect the members of a type at runtime. The Type.GetMembers() method returns an array of MemberInfo objects representing all public members of a type, including fields, properties, methods, constructors, and events.

Syntax

Following is the syntax to get all members of a type −

Type type = typeof(ClassName);
MemberInfo[] members = type.GetMembers();

To filter members using binding flags −

MemberInfo[] members = type.GetMembers(BindingFlags.Public | BindingFlags.Instance);

Using GetMembers() Without Binding Flags

When called without parameters, GetMembers() returns all public members including inherited ones from Object class −

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.GetMembers();
            Console.WriteLine("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 ToString()
 Boolean Equals(System.Object)
 Int32 GetHashCode()
 System.Type GetType()
 Void .ctor()
 System.String SubName
FieldInfo = System.String SubName

Using GetMembers() with Binding Flags

You can use BindingFlags to control which members are returned. This provides more precise control over the reflection query −

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.GetMembers(BindingFlags.Public | BindingFlags.Instance);
            Console.WriteLine("Public Instance 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 −

Public Instance Members = 
 System.String ToString()
 Boolean Equals(System.Object)
 Int32 GetHashCode()
 System.Type GetType()
 Void .ctor()
 System.String SubName
FieldInfo = System.String SubName

Filtering Different Member Types

You can also filter by specific member types and access levels −

using System;
using System.Reflection;

public class Demo {
    public static void Main() {
        Type type = typeof(Employee);
        
        // Get only declared members (not inherited)
        MemberInfo[] declaredMembers = type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        
        Console.WriteLine("Declared Members Only:");
        foreach (var member in declaredMembers) {
            Console.WriteLine(" {0} - {1}", member.MemberType, member.Name);
        }
    }
}

public class Employee {
    public string Name = "John";
    private int age = 30;
    public void Display() { }
    private void Calculate() { }
}

The output of the above code is −

Declared Members Only:
 Field - Name
 Field - age
 Method - Display
 Method - Calculate
 Constructor - .ctor

Common Binding Flags

Binding Flag Description
Public Include public members
NonPublic Include non-public members (private, protected, internal)
Instance Include instance members
Static Include static members
DeclaredOnly Include only members declared in this type (not inherited)

Conclusion

The GetMembers() method is a powerful reflection feature that allows you to inspect type members at runtime. Use binding flags to control which members are returned, making it useful for dynamic programming scenarios, serialization, and debugging tools.

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

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements