【问题标题】:GetType().GetMethods returns no methods when using a BindingFlagGetType().GetMethods 在使用 BindingFlag 时不返回任何方法
【发布时间】:2011-02-17 14:55:18
【问题描述】:

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

this.GetType().GetMethods()

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

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

这会导致返回零结果。然后我开始玩弄,我无法让GetMethods(BindingFlags.x) 的任何覆盖工作。

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

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

【问题讨论】:

    标签: c# reflection


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2012-09-10
      • 2018-02-07
      相关资源
      最近更新 更多