IList to DataTable Convert Module

|

using System.ComponentModel; using System.Data; public static DataTable toDataTable(this IList data, string tableName) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T)); DataTable table = new DataTable(); foreach (PropertyDescriptor prop in props) { table.Columns.Add(prop.Name, prop.PropertyType); } object[] values = new object[props.Count]; foreach (T item in data) { for (int i = 0; i < values.Length; i++) { values[i] = props[i].GetValue(item); } table.Rows.Add(values); } table.TableName = tableName; return table; }


function type에 static 템플릿(<T>)이 들어 있기 때문에 일반 public class로 컴파일시 에러가 뜰 것이다.

이 경우 class 자체도 static으로 바꿔 주면 문제가 없겠다.


And