GetType().GetMethods在使用BindingFlag时不返回任何方法

Kal*_*exx 23 c# reflection

所以我试图检索我的类中具有特定属性的所有私有方法.当我做

this.GetType().GetMethods()
Run Code Online (Sandbox Code Playgroud)

这将返回18个方法,所有方法都是公共的.所以我尝试修改它以使用如下的绑定标志:

this.GetType().GetMethods(BindingFlags.NonPublic);
Run Code Online (Sandbox Code Playgroud)

这导致零结果返回.然后我开始玩游戏,我无法完成任何GetMethods(BindingFlags.x)工作.

this.GetType().GetMethods(BindingFlags.Default);
this.GetType().GetMethods(BindingFlags.Public);
Run Code Online (Sandbox Code Playgroud)

所有这些都返回零结果.我究竟做错了什么?

Fré*_*idi 41

您应该传递BindingFlags.Instance以匹配实例方法:

this.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
Run Code Online (Sandbox Code Playgroud)

BindingFlags.Static如果需要实例和静态方法,也可以添加到标志.