BindingFlags Enumeration

Overview

Represents the set of binding attributes that can be used to control the reflection search for members and types.

BindingFlags is an enumeration that allows you to specify detailed criteria when searching for methods, properties, fields, events, and constructors. These flags are used with methods like Type.GetMethod, Type.GetProperty, Type.GetField, and others. You can combine multiple flags using the bitwise OR operator (|).

Syntax

public enum BindingFlags

The BindingFlags enumeration is defined in the System.Reflection namespace.

Fields

Name Description
Default Specifies default binding behavior.
IgnoreCase Specifies that the search is case-insensitive.
DeclaredOnly Specifies that only members declared by the type itself are returned. Base types are not searched.
Instance Specifies that instance members are included in the search.
Static Specifies that static members are included in the search.
Public Specifies that public members are included in the search.
NonPublic Specifies that non-public members are included in the search.
FlattenHierarchy Specifies that public and protected static members up to, but not including, the declared type are included. Also specifies that public and protected instance members up to, but not including, the declared type are included.
InvokeMethod Specifies that public and protected methods on non-public classes are returned.
CreateInstance Specifies that public methods on non-public classes are returned.
GetField Specifies that public fields on non-public classes are returned.
SetField Specifies that public fields on non-public classes are returned.
GetProperty Specifies that public properties on non-public classes are returned.
SetProperty Specifies that public properties on non-public classes are returned.
PutDispProperty Not supported. Throws NotSupportedException.
ExactBinding Specifies that only members that exactly match the specified signature are returned.
OptionalParamMask Specifies that optional parameters are considered.
Ordinal Specifies that the parameter is an ordinal parameter.

Remarks

When searching for members, you typically combine flags to narrow down the results. For example, to find all public instance methods, you would use BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly. If you want to include inherited members, omit DeclaredOnly.

The IgnoreCase flag is useful when you are unsure of the exact casing of a member's name.

The FlattenHierarchy flag is particularly useful for getting static members from base classes.

Examples

The following example demonstrates how to use BindingFlags to retrieve public instance methods of a class.

using System;
using System.Reflection;

public class MyClass
{
    public void PublicInstanceMethod()
    {
        Console.WriteLine("Hello from PublicInstanceMethod");
    }

    private void PrivateInstanceMethod()
    {
        Console.WriteLine("Hello from PrivateInstanceMethod");
    }

    public static void PublicStaticMethod()
    {
        Console.WriteLine("Hello from PublicStaticMethod");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Type myType = typeof(MyClass);

        // Get all public instance methods
        MethodInfo[] publicInstanceMethods = myType.GetMethods(
            BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

        Console.WriteLine("Public Instance Methods:");
        foreach (MethodInfo method in publicInstanceMethods)
        {
            Console.WriteLine($"- {method.Name}");
        }

        // Get all public static methods
        MethodInfo[] publicStaticMethods = myType.GetMethods(
            BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);

        Console.WriteLine("\nPublic Static Methods:");
        foreach (MethodInfo method in publicStaticMethods)
        {
            Console.WriteLine($"- {method.Name}");
        }

        // Get a specific private instance method (requires NonPublic)
        MethodInfo privateMethod = myType.GetMethod("PrivateInstanceMethod",
            BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

        if (privateMethod != null)
        {
            Console.WriteLine("\nFound private method: " + privateMethod.Name);
            // To invoke, you would need an instance and reflection invocation:
            // object obj = Activator.CreateInstance(myType);
            // privateMethod.Invoke(obj, null);
        }
    }
}

Requirements

Namespace: System.Reflection

Assembly: mscorlib.dll, System.Runtime.dll (for .NET Core and .NET 5+)