Did You Know? Type Member Lookup by Prefix

You can look up for prefixed members, using reflection, by placing an asterisk after the prefix:

MemberInfo[] members = typeof(A).GetMember("hidden*",
BindingFlags.NonPublic | BindingFlags.Instance);
// members now contains three members: hiddenFlag1, hiddenFlag2 and hiddenMethod

// ...
public class A
{
private bool hiddenFlag1;
private bool hiddenFlag2;
private void hiddenMethod()
{
}
private void reallyHiddenMethod()
{
}
}

3 thoughts on “Did You Know? Type Member Lookup by Prefix

  1. Cool. I didn’t know that.
    Of course, it’s not VERY useful, other than encouraging building pseudo-interfaces (“Every method dealing with customers starts with ‘CU’!”) which are probably a bad idea, but still. Cool.

Leave a reply to Steven Cancel reply