加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net – 验证对象是否具有某个属性

发布时间:2020-12-17 00:08:46 所属栏目:大数据 来源:网络整理
导读:我找到了它的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
我找到了它的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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读