【问题标题】:Is it possible to detect wheter a MethodInfo object refers to a sub or a function?是否可以检测 MethodInfo 对象是指子对象还是函数?
【发布时间】:2018-09-23 16:33:53
【问题描述】:

我想区分这两种情况,例如在这段代码中:

For Each method As Reflection.MethodInfo In Type.GetType("WindowsApplication2.clsTest").GetMethods()
    Select Case method.Name
        Case "ToString", "Equals", "GetHashCode", "GetType"
        Case Else
            Debug.Print(method.GetType().ToString)
    End Select
Next

或者,更好的是,是否可以只对 subs 或函数进行迭代?

谢谢。

【问题讨论】:

  • 大概ReturnType 属性会告诉您它返回什么类型,虽然我没有测试过,但我希望该属性将是Nothing 用于Sub。跨度>
  • @jmcilhinney Sub 的 ReturnType 是 Void,而不是 Nothing,因为它在 .NET 框架本身内部使用。

标签: .net vb.net reflection


【解决方案1】:

MethodInfo 类有一个ReturnType 属性,Sub 的返回类型是Void,所以你可以这样做:

For Each method As Reflection.MethodInfo In Type.
                    GetType("WindowsApplication2.clsTest").GetMethods().
                        Where(Function(m) m.ReturnType IsNot GetType(Void))

    Select Case method.Name
        Case "ToString", "Equals", "GetHashCode", "GetType"
        Case Else
            Debug.Print(method.Name)
    End Select
Next

【讨论】:

  • 太棒了!非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 2011-08-18
  • 2018-04-10
  • 2017-06-27
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
相关资源
最近更新 更多