验证对象是否具有某个属性

时间:2012-02-22 17:27:14

标签: vb.net

我为它找到了C#代码here

所以我试过

Public Function checkProperty(ByVal objectt As Object, ByVal propertyy As String) As Boolean
    Dim type As Type = objectt.GetType
    Return type.GetMethod(propertyy)
End Function

但它在type.GetMethod(propertyy) "Value of type 'System.Reflection.MethodInfo' cannot be converted to 'Boolean'."

时抛出错误

怎么办?

4 个答案:

答案 0 :(得分:15)

首先,C#代码检查是否存在方法,而不是属性。其次,C#代码将返回值与null进行比较:

Public Function checkProperty(ByVal objectt As Object, ByVal propertyy As String) As Boolean
    Dim type As Type = objectt.GetType
    Return type.GetProperty(propertyy) IsNot Nothing
End Function

编辑要检查字段,请按以下方式更改方法:

Public Function checkField(ByVal objectt As Object, ByVal fieldName As String) As Boolean
    Dim type As Type = objectt.GetType
    Return type.GetField(fieldName) IsNot Nothing
End Function

答案 1 :(得分:4)

它正在返回MethodInfo,您可以按照以下方式更改它:

Public Function checkProperty(ByVal objectt As Object, ByVal propertyy As String) As Boolean
    Dim type As Type = objectt.GetType
    Return type.GetMethod(propertyy) IsNot Nothing
End Function

答案 2 :(得分:0)

您正在尝试返回type.GetMethod(propertyy),其中示例代码返回评估该方法是否为null的结果。

尝试Return type.GetMethod(propertyy) isnot nothing

答案 3 :(得分:0)

通过拆分行:

        Return type.GetMethod(propertyy) IsNot Nothing

将dasblinkenlight分为三行;

        Dim info As System.Reflection.PropertyInfo = type.GetProperty(propertyy)
        Dim reallyExists As Boolean = info IsNot Nothing
        Return reallyExists

函数checkProperty将在现有属性上返回true。

我无法评论dasblinkenlight的答案, 由于我的声誉还不到50, 因此,请将我对他的回答的上述改进作为新的回答。 我对此不满意,因为它使stackoverflow上的页面可读性降低。 为了使这个问题成为真正的答案,我加入了我创建的模块, 其中的行

Public Function propertyExists( _

通过

End Function

是对natli和dasblinkenlight函数checkProperty的代码的改进。

请参见Verify that an object has a certain property 关于纳特利的问题,我正在回答。 参见https://stackoverflow.com/posts/9399928/revisions和natli问题 我要评论的是dasblinkenlight的答案。

顺便说一句:“正如您将在下面看到的,我宁愿不使用系统名称空间, 这样我就可以直接看到引用了哪些系统功能。”

以下是我为使用此模块而创建的名为net2Module的模块;

''' <summary>
''' This module with the name net2Module contains
''' tools that need at least .NET Framework 2.0.
''' This module needs System.Reflection.
''' </summary>
Public Module net2Module

''' <summary>
''' If one of the tools fails, the property exceptionMessage will
''' not be nothing, but contain an exception.
''' Each tool will set exceptionMessage to nothing or an exception.
''' </summary>
Public exceptionMessage As System.Exception = Nothing

''' <summary>
''' Checks if a property with some name exist in an object.
''' This function needs System.Reflection.
''' </summary>
''' <param name="objectt">The object.</param>
''' <param name="propertyy">The name of the property.</param>
''' <returns>True if the property exists.</returns>
Public Function propertyExists( _
        objectt As Object, _
        ByVal propertyy As String _
        ) As Boolean
    Try
        exceptionMessage = Nothing
        Dim type As System.Type = objectt.GetType
        Dim info As System.Reflection.PropertyInfo = _
            type.GetProperty(propertyy)
        Dim reallyExists As Boolean = info IsNot Nothing
        Return reallyExists
    Catch ex As System.Exception
        exceptionMessage = ex
        Return False
    End Try
End Function ' propertyExists
End Module ' net2Module

在下面的代码中,我成功地按顺序使用了我的函数propertyExists 递归关闭或隐藏子表单;

Friend Module sharedEnums
Friend Enum objectNamesEnum
    formHandlingClass
    calledForms
    ownedForms
End Enum ' objectNamesEnum

Friend Enum recursiveFormTypesEnum
    calledForms
    ownedForms
End Enum ' recursiveFormTypesEnum

Friend Enum recursiveActionsEnum
    hideForms
    closeForms
End Enum ' recursiveActionsEnum
End Module ' sharedEnums

Friend Class recursiveClass

Friend recursiveFormTypes As New recursiveFormTypesEnum

Friend recursiveActions As New recursiveActionsEnum

Friend Sub hideOrCloseFormsRecursively( _
        formsToHandle As System.Windows.Forms.Form())
    If Not formsToHandle Is Nothing Then
        Dim formToHandle As System.Windows.Forms.Form = Nothing
        Dim propertyToExist As String = String.Empty
        If Me.recursiveFormTypes = recursiveFormTypesEnum.calledForms Then
            propertyToExist = objectNamesEnum.calledForms.ToString
        Else ' Me.recursiveFormTypes = recursiveFormTypesEnum.ownedForms
            propertyToExist = objectNamesEnum.ownedForms.ToString
        End If
        For Each formToHandle In formsToHandle
            Try ' Recurse through the forms to handle
                Dim formObject As Object = formToHandle
                If net2Module.propertyExists(formObject, _
                        objectNamesEnum.formHandlingClass.ToString) Then
                    If net2Module.propertyExists( _
                            formObject.formHandlingClass, propertyToExist) Then
                        If Me.recursiveFormTypes = _
                                recursiveFormTypesEnum.calledForms Then
                            Call Me.hideOrCloseFormsRecursively( _
                                formObject.formHandlingClass.calledForms.ToArray)
                        Else ' Me.recursiveFormTypes = recursiveFormTypesEnum.ownedForms
                            Call Me.hideOrCloseFormsRecursively( _
                                formObject.formHandlingClass.ownedForms)
                        End If
                    End If
                End If
                If net2Module.propertyExists(formObject, propertyToExist) Then
                    If Me.recursiveFormTypes = _
                            recursiveFormTypesEnum.calledForms Then
                        Call Me.hideOrCloseFormsRecursively( _
                            formObject.calledForms.ToArray)
                    Else ' Me.recursiveFormTypes = recursiveFormTypesEnum.ownedForms
                        Call Me.hideOrCloseFormsRecursively( _
                            formObject.ownedForms)
                    End If
                End If
            Catch
            End Try
            Try ' Take the action to take on each found form
                If Me.recursiveActions = _
                        recursiveActionsEnum.hideForms Then
                    Call formToHandle.Hide()
                Else ' Me.recursiveActions = recursiveActionsEnum.closeForms
                    Call formToHandle.Close()
                End If
            Catch
            End Try
        Next
    End If
End Sub ' hideOrCloseFormsRecursively

End Class ' recursiveClass

无论您是否得到我的帮助,我都会很高兴收到您的来信。 我是荷兰人,所以也想对我的英语发表评论,以便我改善它。