Home > database >  How to get property of a class using GetValue
How to get property of a class using GetValue

Time:12-14

I have a couple of classes with properties. I would like to be able to iterate through the properties of the class and get the value of the property. I included a sample of the class along with how I am looping the properties. Need help with understanding the usage of "GetValue" and "SetValue"

Private Sub loadValues()
    Dim mySample As New Sample
    Dim props As PropertyInfo() = mySample.GetType().GetProperties()

    mySample.Test2 = True
    Console.WriteLine(mySample.Test2)

    For Each prop In props
        Console.WriteLine(prop.Name)
        'This will loop through all the properties in the Sample clasee
        'How can I get and change the value of each of the properties?

        prop.GetValue(mySample, Nothing) '?????
        prop.SetValue(mySample, False, Nothing) '?????
    Next

End Sub

Here is the sample of the class

Public Class Sample
    Public Name As String = "Sample"
    Public _Cut As Boolean = False
    Private _Test1 As Boolean
    Private _Test2 As Boolean
    Private _Test3 As Boolean
    Public Property Cut(ByVal CabType As String) As Boolean
        Get
            Return _Cut
        End Get
        Set(ByVal value As Boolean)
            _Cut = value
        End Set
    End Property
    Public Property Test1() As Boolean
        Get
            Return _Test1
        End Get
        Set(ByVal value As Boolean)
            _Test1 = value
        End Set
    End Property

    Public Property Test2() As Boolean
        Get
            Return _Test2
        End Get
        Set(ByVal value As Boolean)
            _Test2 = value
        End Set
    End Property

    Public Property Test3() As Boolean
        Get
            Return _Test3
        End Get
        Set(ByVal value As Boolean)
            _Test3 = value
        End Set
    End Property

End Class

CodePudding user response:

The Cut property is indexed, so you need to pass some index value to PropertyInfo.GetValue. You may see that the signature of GetValue is

Public Overridable Function GetValue(obj As Object, index() As Object) As Object

so the index argument needs to be an array when indexed. Simply pass your Nothing as a single element index {Nothing} in that case. You can tell if there are index parameters by checking if PropertyInfo.GetIndexParameters() has anything

Private Sub loadValues()
    Dim mySample As New Sample
    Dim props As PropertyInfo() = mySample.GetType().GetProperties()

    mySample.Test2 = True
    Console.WriteLine(mySample.Test2)

    For Each prop In props
        Console.WriteLine(prop.Name)
        If prop.GetIndexParameters().Any() Then
            prop.GetValue(mySample, {Nothing})
            prop.SetValue(mySample, False, {Nothing})
        Else
            prop.GetValue(mySample, Nothing)
            prop.SetValue(mySample, False, Nothing)
        End If
    Next
End Sub

However, having an indexed property then iterating through all the properties arbitrarily may not make sense when you need to pass a specific index to properly access the property.

  • Related