Public Function ListObjectToDataSet(Of T)(ByVal list As IList(Of T)) As DataSet
Dim elementType As Type
Dim ds As New DataSet()
Dim t1 As New DataTable()
Try
elementType = GetType(T)
ds.Tables.Add(t1)
'add a column to table for each public property on T
For Each propInfo In elementType.GetProperties()
Dim ColType As Type = If(Nullable.GetUnderlyingType(propInfo.PropertyType), propInfo.PropertyType)
t1.Columns.Add(propInfo.Name, ColType)
Next
'go through each property on T and add each value to the table
For Each item As T In list
Dim row As DataRow = t1.NewRow()
For Each propInfo In elementType.GetProperties()
row(propInfo.Name) = If(propInfo.GetValue(item, Nothing), DBNull.Value)
Next
t1.Rows.Add(row)
Next
Return ds
Catch ex As Exception
Return Nothing
End Try
End Function
No hay comentarios.:
Publicar un comentario