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

时间:2018-01-11 12:26:01

标签: vb.net-2010

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

1 个答案:

答案 0 :(得分:0)

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

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