I need to see if a MemberInfo
matches a particular BindingFlags
. The closest method to this is Type#GetMember(string, BindingFlags)
.
I cannot find any method to do this.
I want to do something like this:
private List<MemberInfo> _members;
public IEnumerable<MemberInfo> GetMembers(BindingFlags flags)
{
foreach(var member in _members)
{
if(member.MatchesFlags(flags))
{
yield return member;
}
}
}
Actual types of
MemberInfo
obtained with reflection (e.g. via callingMemberInfo[] GetMembers()
onType
) are:System.Reflection.RuntimeMethodInfo
System.Reflection.RuntimeConstructorInfo
System.Reflection.RuntimePropertyInfo
System.Reflection.RtFieldInfo
All of them independently have property
BindingFlags
of typeBindingFlags
. But the property is NonPublic. And the types areinternal sealed
, that makes them inaccessible in normal code. Herereflection
comes to rescue. To getBindingFlags
of aMemberInfo
one can use such an extension:To find matches with
BindingFlags
the following helper methods can be used:To test the solution one can use a class like this:
Then running:
gives: