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
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
setupAction<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
excludePrivateboolif set to
truenon-public members are excluded from the binding constraint.excludeStaticboolif set to
truestatic members are excluded from the binding constraint.excludeInheritancePathboolif set to
truederived members of a type's inheritance path are excluded from the binding constraint.excludePublicboolif set to
truepublic 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
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
setupAction<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
mrMemberReflectionThe MemberReflection to convert.
Returns
- BindingFlags
A BindingFlags that is equivalent to
mr.