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()
{
}
}
Advertisements

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s