如何从 vb.net 类的所有成员中查找空白值



我有第三方对象,其中包含如此多的整数,字符串和布尔值的成员。我想更新值不为 null 或空白的记录

您可以使用反射来实现您想要的:

Sub Main()
    Dim obj As Test = new Test()
    Dim type As Type = GetType(Test)
    Dim info As PropertyInfo() = type.GetProperties()
    For Each propertyInfo As PropertyInfo In info
        Dim value As String = propertyInfo.GetValue(obj)
        If propertyInfo.PropertyType = GetType(String) And String.IsNullOrEmpty(value)
            ' empty value for this string property
        End If
    Next
End Sub
public Class Test
    Public Property Test As String
End Class

最新更新