Table of Contents

Class MemberReflection

Namespace
Cuemon.Reflection
Assembly
Cuemon.Core.dll

Provides a robust way specifying binding constraints for reflection based member searching.

public class MemberReflection
Inheritance
MemberReflection

Examples

The following example demonstrates how to use to create custom for reflection-based member discovery.

using System;
using System.Reflection;
using Cuemon.Reflection; // for MemberReflection, MemberReflectionOptions

namespace MyApp.Examples;

public class MemberReflectionExample
{
    public void Demonstrate()
    {
        // Create flags to find only public instance members (excluding inherited)
        BindingFlags flags = new MemberReflection(
            excludePrivate: true,
            excludeStatic: true,
            excludeInheritancePath: true);
        Console.WriteLine(flags);
        // Output: Instance, Public, DeclaredOnly

        // Use the static CreateFlags factory method
        BindingFlags allFlags = MemberReflection.CreateFlags();
        Console.WriteLine(allFlags);
        // Output: Instance, Static, Public, NonPublic

        // Configure via MemberReflectionOptions
        BindingFlags customFlags = MemberReflection.CreateFlags(o =>
        {
            o.ExcludePrivate = true;
            o.ExcludeStatic = true;
        });

        // Use with reflection
        var members = typeof(string).GetMembers(customFlags);
        Console.WriteLine(members.Length); // Number of public instance members on string

}
}

Constructors

MemberReflection(Action<MemberReflectionOptions>)

Initializes a new instance of the MemberReflection class.

public MemberReflection(Action<MemberReflectionOptions> setup)

Parameters

setup Action<MemberReflectionOptions>

The MemberReflectionOptions which need to be configured.

MemberReflection(bool, bool, bool, bool)

Initializes a new instance of the MemberReflection class.

public MemberReflection(bool excludePrivate = false, bool excludeStatic = false, bool excludeInheritancePath = false, bool excludePublic = false)

Parameters

excludePrivate bool

if set to true non-public members are excluded from the binding constraint.

excludeStatic bool

if set to true static members are excluded from the binding constraint.

excludeInheritancePath bool

if set to true derived members of a type's inheritance path are excluded from the binding constraint.

excludePublic bool

if set to true public members are excluded from the binding constraint.

Fields

Everything

Defines a binding constraint that allows searching all members of a given type.

public const BindingFlags Everything = Instance | Static | Public | NonPublic

Field Value

BindingFlags

Properties

Flags

Gets the binding constraint of this instance.

public BindingFlags Flags { get; }

Property Value

BindingFlags

The binding constraint of this instance.

Methods

CreateFlags(Action<MemberReflectionOptions>)

Creates the binding constraint needed for reflection using the optional setup to reduce the scope. Default is Everything.

public static BindingFlags CreateFlags(Action<MemberReflectionOptions> setup = null)

Parameters

setup Action<MemberReflectionOptions>

The MemberReflectionOptions that may be configured.

Returns

BindingFlags

The binding constraint as defined by the setup.

Operators

implicit operator BindingFlags(MemberReflection)

Performs an implicit conversion from MemberReflection to BindingFlags.

public static implicit operator BindingFlags(MemberReflection mr)

Parameters

mr MemberReflection

The MemberReflection to convert.

Returns

BindingFlags

A BindingFlags that is equivalent to mr.