public static DataTable IListToDataTable(IList list)
{
DataTable dataTable = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] properties = list[0].GetType().GetProperties();
PropertyInfo[] array = properties;
for (int i = 0; i < array.Length; i++)
{
PropertyInfo propertyInfo = array[i];
Type type = propertyInfo.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
type = type.GetGenericArguments()[0];
}
dataTable.Columns.Add(propertyInfo.Name, type);
}
for (int j = 0; j < list.Count; j++)
{
ArrayList arrayList = new ArrayList();
array = properties;
for (int i = 0; i < array.Length; i++)
{
object value = array[i].GetValue(list[j], null);
arrayList.Add(value);
}
object[] values = arrayList.ToArray();
dataTable.LoadDataRow(values, true);
}
}
return dataTable;
}