I am trying to create a generaized function to manipulate a list object. Below is a simple use of relection that takes the LeadList list object and returns puts in the values into a DataTable. But I get stuck. I want to be able to pass my function any list object without having to declare the specific object type in the function call. Then determine the object type and continue. The 2 areas that I need help on the reflection with are highlighted in red and bolded below.
Any help is appreciated.
Function
ListToTable(ByVal List As CRMBO.LeadList) As DataTable Dim tbl As New DataTableDim col As DataColumn
Dim type As Type = List(0).GetType()
Dim properties() As System.Reflection.PropertyInfo = type.GetProperties()
For i As Integer = 0 To properties.GetUpperBound(0)
col = New DataColumn
col.ColumnName = properties(i).Name.ToString()
tbl.Columns.Add(col)
Next i
Dim row As DataRow
For Each info As CRMBO.LeadInfo In List
row = tbl.NewRow()
For i As Integer = 0 To properties.GetUpperBound(0)
row(i) = info.GetType.GetProperty(properties(i).Name).GetValue(info, Nothing).ToString()
Next i
tbl.Rows.Add(row)
Next info
Return tbl
End Function
Copyright (c) Marimer LLC