Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetType().GetMethods returns no methods when using a BindingFlag

Tags:

So I am trying to retrieve all private methods in my class that have a specific attribute. When I do

this.GetType().GetMethods() 

This returns 18 methods, all of which are public. So I tried to modify it to use Binding flags like:

this.GetType().GetMethods(BindingFlags.NonPublic); 

This causes zero results to come back. I then started playing around and I can't get any overrides of GetMethods(BindingFlags.x) to work.

this.GetType().GetMethods(BindingFlags.Default); this.GetType().GetMethods(BindingFlags.Public); 

All of those return zero results. What am I doing wrong?

like image 226
KallDrexx Avatar asked Feb 17 '11 14:02

KallDrexx


1 Answers

You should pass BindingFlags.Instance in order to match instance methods:

this.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); 

You can also add BindingFlags.Static to the flags if you want both instance and static methods.

like image 92
Frédéric Hamidi Avatar answered Oct 17 '22 07:10

Frédéric Hamidi