Hi,
I'm using Reflection for this task : object.GetType.GetProperties() gives me array of PropertyInfo objects, then [propertyinfo instance].PropertyType gives the data types, [propertyinfo instance].GetValue(....) can read object values - with such info make two simple loops - first for creating DataTable structure, second - to put data from your objects into datatable.
Something like this (vb code, should be no problem with conversion to c#)
Code:
dim dt as new DataTable
dim pis as PropertyInfo()=myObjectArray(0).GetType.GetProperties()
For Each pi As PropertyInfo In pis
Dim name As String = pi.Name
Select Case pi.PropertyType.Name
Case "Int32", "String", .....
dt.Columns.Add(name, pi.PropertyType)
End Select
Next
for each obj as myObjectType in myObjectArray
For Each pi As PropertyInfo In pis
Dim name As String = pi.Name
Select Case pi.PropertyType.Name
Case "Int32", "String", .....
dim dr as DataRow=dt.NewRow
dr(name)=pi.GetValue(obj, Nothing)
End Select
Next
dt.rows.add(dr)
next