0
0
CsharpHow-ToBeginner · 4 min read

How to Get Methods of a Class Using Reflection in C#

In C#, you can get the methods of a class using Type.GetMethods() from the System.Reflection namespace. This returns an array of MethodInfo objects representing each method of the class.
📐

Syntax

The main syntax to get methods of a class using reflection is:

  • Type type = typeof(YourClass); — gets the Type object for the class.
  • MethodInfo[] methods = type.GetMethods(); — retrieves all public methods of the class.

You can also use binding flags to control which methods to get, such as non-public or static methods.

csharp
Type type = typeof(YourClass);
MethodInfo[] methods = type.GetMethods();
💻

Example

This example shows how to get and print all public methods of a sample class using reflection.

csharp
using System;
using System.Reflection;

public class SampleClass
{
    public void MethodA() { }
    public int MethodB(int x) { return x; }
    private void HiddenMethod() { }
}

class Program
{
    static void Main()
    {
        Type type = typeof(SampleClass);
        MethodInfo[] methods = type.GetMethods();

        foreach (var method in methods)
        {
            Console.WriteLine(method.Name);
        }
    }
}
Output
MethodA MethodB Equals GetHashCode GetType ToString
⚠️

Common Pitfalls

By default, GetMethods() returns only public instance and static methods, including inherited ones. Common mistakes include:

  • Expecting private or protected methods to appear without specifying binding flags.
  • Not filtering out inherited methods if only declared methods are needed.

To get private or non-public methods, use GetMethods(BindingFlags.NonPublic | BindingFlags.Instance). To get only methods declared in the class (not inherited), add BindingFlags.DeclaredOnly.

csharp
using System;
using System.Reflection;

class MyClass
{
    private void PrivateMethod() { }
    public void PublicMethod() { }
}

class Program
{
    static void Main()
    {
        Type type = typeof(MyClass);

        // Wrong: only public methods
        var publicMethods = type.GetMethods();

        // Right: include private instance methods
        var privateMethods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);

        Console.WriteLine("Public methods:");
        foreach (var m in publicMethods) Console.WriteLine(m.Name);

        Console.WriteLine("Private methods:");
        foreach (var m in privateMethods) Console.WriteLine(m.Name);
    }
}
Output
Public methods: PublicMethod Equals GetHashCode GetType ToString Private methods: PrivateMethod
📊

Quick Reference

MethodDescription
GetMethods()Returns all public instance and static methods, including inherited ones.
GetMethods(BindingFlags flags)Returns methods filtered by binding flags (e.g., NonPublic, Instance, Static, DeclaredOnly).
BindingFlags.PublicIncludes public methods.
BindingFlags.NonPublicIncludes private, protected, and internal methods.
BindingFlags.InstanceIncludes instance methods.
BindingFlags.StaticIncludes static methods.
BindingFlags.DeclaredOnlyIncludes only methods declared on the specified class, excluding inherited.

Key Takeaways

Use Type.GetMethods() to get all public methods of a class in C#.
Use BindingFlags to include non-public, static, or declared-only methods.
Reflection returns MethodInfo objects that describe each method.
Inherited methods appear by default unless filtered with BindingFlags.DeclaredOnly.
Always specify BindingFlags explicitly to get the exact methods you need.