Make sure the object has a specific property.

I found C # code for it here

So i tried

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

But he gives an error in type.GetMethod(propertyy)saying"Value of type 'System.Reflection.MethodInfo' cannot be converted to 'Boolean'."

What to do?

+5
source share
3 answers

First, C # code checks for a method, not a property. Secondly, C # code compares the return to 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

EDIT To check the fields, modify the method as follows:

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
+15
source

it returns MethodInfo, and you can just change it like this:

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
+4
source

type.GetMethod(propertyy), , .

Return type.GetMethod(propertyy) isnot nothing

0

All Articles