gpt4 book ai didi

vb.net - 验证对象是否具有特定属性

转载 作者:行者123 更新时间:2023-12-03 00:51:57 25 4
gpt4 key购买 nike

我找到了它的 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) 处抛出错误,提示“‘System.Reflection.MethodInfo’类型的值无法转换为‘Boolean’。”

要做什么?

最佳答案

首先,C# 代码检查是否存在方法,而不是属性。其次,C# 代码将 return 与 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

关于vb.net - 验证对象是否具有特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9399872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com