我在这里找到了C#代码
所以我试过了
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
Run Code Online (Sandbox Code Playgroud)
但是,在抛出一个错误type.GetMethod(propertyy)说法"Value of type 'System.Reflection.MethodInfo' cannot be converted to 'Boolean'."
该怎么办?
das*_*ght 16
首先,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
Run Code Online (Sandbox Code Playgroud)
编辑要检查字段,请按如下所示更改方法:
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
Run Code Online (Sandbox Code Playgroud)
它正在返回 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
Run Code Online (Sandbox Code Playgroud)