q1lrdg
Last Updated: February 25, 2016
·
6.014K
· tlom

VB.NET Convert Custom Object in a Dictionary(Of String, String)

How to convert your fantastic custom Object in Dictionary Of String, String
(maybe useful for csv creating)

Public Class MyObject

'some code ...

Public Function toDictionary() As Dictionary(Of String, String)
    Dim d As New Dictionary(Of String, String)
    For Each p As System.Reflection.PropertyInfo In Me.GetType.GetProperties()
        Try
            d.Add(p.Name, p.GetValue(Me, Nothing).ToString)
        Catch ex As Exception
            'write error message
            d.Add(p.Name, ex.Message)
        End Try
    Next
    Return d
End Function

'other code ...

End Class

Say Thanks
Respond