是否有可能检测到MethodInfo对象引用的是子对象还是函数?

时间:2018-09-23 16:33:54

标签: .net vb.net reflection

我想区分这两种情况,例如以下代码:

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

或者更好的是,是否可以仅对子项或函数进行迭代?

谢谢。

1 个答案:

答案 0 :(得分: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