【问题标题】:How to find private static methods in static class using reflection?如何使用反射在静态类中找到私有静态方法?
【发布时间】:2019-09-02 22:04:49
【问题描述】:

我有一个静态类,我想使用 typeof(MyStaticClass).GetMethods() 找到它的私有静态方法,但它总是只显示公共方法。

我怎样才能做到这一点?

【问题讨论】:

  • 你看到BindingFlags-overload了吗?只需使用GetMethods(BindingFlags.Static)

标签: c# methods reflection static


【解决方案1】:

使用包含BindingFlags 参数的GetMethods 的重载:

var methods = typeof(MyStaticClass)
    .GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

(我没有包含BindingFlags.Instance,因为您明确表示它是一个静态类;要查找任何类中的所有方法,也应包含它。)

【讨论】:

  • 它有效,我没有使用组合标志。谢谢@乔恩
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
相关资源
最近更新 更多