Reflection of object types when passing into function

Reflection of object types when passing into function

Old forum URL: forums.lhotka.net/forums/t/5328.aspx


rbellow posted on Friday, August 29, 2008

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 DataTable

Dim 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

ajj3085 replied on Friday, August 29, 2008

The class which contains your method should be a Generic class, with two genertic type parameters.  One for the list type, one for the type that is contained in the list.. just like BusinessListBase<T, C>

Copyright (c) Marimer LLC