Converting List T into data table using C# Component Model and Reflection

Converting List <T> into data table using C# Component Model and Reflection

In this blog we will talk about 2 ways to convert List into a data table namely; “System.ComponentModel” and “System.Reflection”.

The component model allows you to execute DLR-type activities, such as runtime-properties. This is how DataView discloses columns to a grid; these are runtime properties, not reflection ones. Keywords to note here are ICustomTypeDescriptor and TypeDescriptionProvider. The model also allows abstraction and indirection. For example, if you are executing heavy reflection on properties, you should consider PropertyInfo; a utility that can be used for implementation of a pre-compiled model for enormous performance boosts.

A generic type parameter allows to specify a subjective type T to a method at compile-time, without specifying any object type declaration and can be used generally. The extension method must be defined with a static keyword.

For example, you have a List of any class “x” and you want it to convert that list into a data table

class myClass
{
public string First_Name {get;set;}
public string Middle_Name { get; set; }
public string Last_Name { get; set; }
public int Age { get; set; }
public myClass(string first_Name,string middle_Name , string last_Name,int age)
{
First_Name = first_Name;
Middle_Name = middle_Name;
Last_Name = last_Name;
Age = age;
}
}
static void Main(string[] args)
{
List myClass = new List();
myClass myClass1 = new myClass("First1", "Middle1", "Last1",25);
myClass myClass2 = new myClass("First2", "Middle2", "Last2",31);
myClass myClass3 = new myClass("First3", "Middle3", "Last3",22);
myClass.Add(myClass1);
myClass.Add(myClass2);
myClass.Add(myClass3);
DataTable datatable = myClass.ToDataTable();
}

 

1. Converting List into data table using “System.ComponentModel”

 

public static DataTable ToDataTable(this List List)
{
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 List)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
}
dataTable.Rows.Add(values);
}
return dataTable;
}

 

2. Converting List into data table using “System. Reflection”

 

public static DataTable ToDataTable1(this List List)
{
DataTable dataTable = new DataTable(typeof(T).Name);

PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ?
Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
dataTable.Columns.Add(prop.Name, type);
}
foreach (T item in List)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
return dataTable;
}

Categories: 
Zia Ullah Baig's picture
Software Engineer at systems limited with more than 3 years of development experience in Microsoft .Net technologies. Muhammad Zia Ullah Baig is based in Karachi and is a regular contributor of the Systems Limited Blog.

Disclaimer: The views expressed here are solely those of the author in his private capacity and do not in any way represent the views of Systems Limited, or any other entity related to Systems Limited.

Add new comment