samedi 19 octobre 2019

Reflection Recursive through object with list(of)

Wanting to use Reflection to get object Name and value. I am able to get through DocumentConfig class but unable to get Fields. Doesn't want to identify the gettype as a list(of) either. If force into the list(of) returns message fielditems is not member of DocumentConfig class.

Public Class MyDocuments
Public Class DocumentConfig
    Private _Name As String
    Public Property Name() As String
    Public Property Fields() As New List(Of FieldItems)
End Class
Public Class FieldItems
    Private _Name As String
    Public Property Name() As String
End Class
End Class

Module Module1
Sub Main()
    Dim Document As New List(Of FieldItems)
    Document.Add(New FieldItems With {.Name = "Invoice"})
    Document.Add(New FieldItems With {.Name = "Statement"})
    Document.Add(New FieldItems With {.Name = "Reminder"})
End Sub
Public Sub DisplayAll(ByVal Someobject As DocumentConfig)
        Dim _type As Type = Someobject.GetType()
        Console.WriteLine(_type.ToString())
        Dim flags As BindingFlags = BindingFlags.Public Or BindingFlags.Instance
        Dim properties() As PropertyInfo = _type.GetProperties(flags)
        For Each _property As PropertyInfo In properties
            Dim valuetype = _property.GetType()
            If [_property].PropertyType = GetType(String) OrElse [_property].PropertyType = GetType(Integer) OrElse [_property].PropertyType = GetType(Boolean) then
                Console.WriteLine("Name: " + _property.Name + ", Value: " + _property.GetValue(Someobject, Nothing))
            ElseIf _property.PropertyType = GetType(List(Of)) Then
                Dim list = [_property].GetValue(Someobject, Nothing)
                For Each item In list
                    DisplayAll(item)
                Next
            Else
                Console.WriteLine("Name: " + _property.Name)
            End If
        Next
    End Sub
End Module




Aucun commentaire:

Enregistrer un commentaire