Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting MethodInfo for protected and public methods only?

Tags:

c#

.net

How can I get the MethodInfo for protected and public methods only?

like image 746
Mark Attwood Avatar asked Aug 03 '10 06:08

Mark Attwood


3 Answers

using System.Linq;
using System.Reflection;

var methods = foo
    .GetType()
    .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
    .Where(m => m.IsFamily || m.IsPublic);
like image 87
abatishchev Avatar answered Nov 04 '22 00:11

abatishchev


There is no direct way. The thing you can do is check IsFamily and IsPublic flag of MethodInfo:

minfo = b.GetType().GetMethod("publicProtectedMember", 
        BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (minfo.IsFamily || minfo.IsPublic)
{
  string s = fd.Member();
}
like image 24
Pranay Rana Avatar answered Nov 04 '22 00:11

Pranay Rana


Well, can't you just get all MemberInfos and filter them out by IsFamily and IsPublic properties?

like image 4
Fyodor Soikin Avatar answered Nov 04 '22 01:11

Fyodor Soikin