using System.Collections.Generic;
using System.ComponentModel;
public static void Main()
Console.WriteLine("Hello World");
public static DataTable ToDataTable(this List<string> iList, string columnName)
DataTable dataTable = new DataTable();
dataTable.Columns.Add(columnName, typeof(string));
iList.ForEach(e => dataTable.Rows.Add(e));
public static DataTable ToDataTable<T>(this List<T> iList)
DataTable dataTable = new DataTable();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
Type type = propertyDescriptor.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = Nullable.GetUnderlyingType(type);
dataTable.Columns.Add(propertyDescriptor.Name, type);
object[] values = new object[propertyDescriptorCollection.Count];
foreach (T iListItem in iList)
for (int i = 0; i < values.Length; i++)
values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
dataTable.Rows.Add(values);