开发者

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

开发者 https://www.devze.com 2023-02-11 11:25 出处:网络
So I am trying to retrieve all private methods in my class that have a specific attribute.When I do this.GetType().GetMethods()

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开发者_StackOverflow中文版 doing wrong?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号