CKTIS / C#
BindingFlags: filtering methods with reflection
Combine lookup flags to select methods by visibility, by whether they are instance or static, and by where they are declared.
Version: C# 12 and .NET 8; later runtimes may add compatible features
Syntax
using System.Reflection;
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
MethodInfo[] methods = typeof(T).GetMethods(flags); // T is the type to inspect.
// Use BindingFlags.Static instead of Instance for static methods,
// or combine Instance | Static to include both.System.Reflection.BindingFlags is a .NET enum marked with [Flags]. Import System.Reflection with using to write the short name BindingFlags. Combine flags with bitwise OR (|), not ||, and pass the result to Type.GetMethods(BindingFlags).
Visibility and method kind are independent filters. Public includes public methods; NonPublic includes nonpublic methods. Instance selects instance methods; Static selects static methods. Supply at least one flag from each pair. You can also supply both: Public | Instance | Static allows public methods of either kind.
DeclaredOnly restricts the lookup to methods declared directly on the inspected type. Without it, eligible inherited instance methods can appear. In a static lookup, FlattenHierarchy makes public and protected static methods from base classes eligible, subject to the visibility filter. It does not include private static methods from base classes.
The overload with flags returns a MethodInfo[]: it describes methods without invoking them. It returns an empty array if either required flag category is missing or if nothing matches. In particular, GetMethods(BindingFlags.Default) is not the same as parameterless GetMethods(), which looks up public methods.
Examples
using System;
using System.Linq;
using System.Reflection;
Type type = typeof(Player);
const BindingFlags publicInstance = BindingFlags.Public | BindingFlags.Instance;
const BindingFlags publicStatic = BindingFlags.Public | BindingFlags.Static;
Print("Instance here", type, publicInstance | BindingFlags.DeclaredOnly);
Print("Static here", type, publicStatic | BindingFlags.DeclaredOnly);
Print("Instance including inherited", type, publicInstance);
Print("Static including inherited", type, publicStatic | BindingFlags.FlattenHierarchy);
static void Print(string label, Type type, BindingFlags flags)
{
var names = type.GetMethods(flags)
// Keep object instance methods, but omit static methods declared on object.
.Where(method => !method.IsStatic || method.DeclaringType != typeof(object))
.Select(method => method.Name)
.OrderBy(name => name, StringComparer.Ordinal);
Console.WriteLine($"{label}: {string.Join(", ", names)}");
}
class BasePlayer
{
public void Reset() { }
public static void Help() { }
}
class Player : BasePlayer
{
public void Play() { }
public static void Create() { }
private void Debug() { }
}Result
Instance here: Play Static here: Create Instance including inherited: Equals, GetHashCode, GetType, Play, Reset, ToString Static including inherited: Create, Help
In this C# 12 and .NET 8 example, `Public` filters out `Debug`. Without `DeclaredOnly`, the instance lookup also finds `Reset` and public instance methods from `object`. `FlattenHierarchy` lets the static lookup find `Help`. That lookup also finds the static `Equals` and `ReferenceEquals` methods declared on `object`, but the code filters them out before printing. The code sorts names explicitly rather than relying on metadata order.
Related lessons
- C# · Lesson 23 · Advanced · Version: 3