using System.Collections.Generic;
public static void Main()
var people = new List<Person>();
people.Add(new Person() { FirstName = "Bill", LastName = "Clinton", Age = 101 });
people.Add(new Person() { FirstName = "John", LastName = "Doe", Age = 102 });
people.Add(new Person() { FirstName = "George", LastName = "Washington", Age = 300 });
dynamic newPeople = people;
var dataParam = new List<DataParam>();
dataParam.Add(new DataParam{ ParamName = "RegistryID", ParamType = "number", ParamValue = 1});
dataParam.Add(new DataParam{ ParamName = "ShortDescription", ParamType = "string", ParamValue = "My brief description"});
dataParam.Add(new DataParam{ ParamName = "QueryBuilderParam", ParamType = "object", ParamValue = new List<dynamic>() {
DataTable dataTableParam = ToDataTable(dataParam[2].ParamValue);
Console.WriteLine("end of dynamic type to dataTableParam");
Console.WriteLine(dataTableParam.Columns.Count);
public static DataTable ToDataTable(dynamic dynamicObject)
string colType = string.Empty,
colResult = string.Empty;
DataTable dataTable = new DataTable();
foreach (var item in dynamicObject)
Type row = item.GetType();
PropertyInfo[] rowPropInfo = row.GetProperties();
Console.WriteLine("rowPropInfo is {0}", rowPropInfo.Length);
dataRow = dataTable.NewRow();
foreach (var prop in rowPropInfo)
colType = prop.PropertyType.ToString();
colName = prop.Name.ToString();
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(Int16));
dataRow[colName] = Convert.ToInt16(prop.GetValue(item, null));
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(Int32));
dataRow[colName] = Convert.ToInt32(prop.GetValue(item, null));
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(Int64));
dataRow[colName] = Convert.ToInt64(prop.GetValue(item, null));
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(Boolean));
dataRow[colName] = Convert.ToBoolean(prop.GetValue(item, null));
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(Decimal));
dataRow[colName] = Convert.ToDecimal(prop.GetValue(item, null));
case "System.Nullable`1[System.Decimal]":
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(Decimal));
dataRow[colName] = string.IsNullOrEmpty(prop.GetValue(item, null).ToString()) ? (Decimal?)null : Convert.ToDecimal(prop.GetValue(item, null));
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(DateTime));
dataRow[colName] = Convert.ToDateTime(prop.GetValue(item, null));
case "System.Nullable`1[System.DateTime]":
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(DateTime));
dataRow[colName] = string.IsNullOrEmpty(prop.GetValue(item, null).ToString()) ? (DateTime?)null : Convert.ToDateTime(prop.GetValue(item, null));
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(char));
dataRow[colName] = Convert.ToChar(prop.GetValue(item, null));
if (!dataTable.Columns.Contains(colName))
dataTable.Columns.Add(colName, typeof(string));
dataRow[colName] = prop.GetValue(item, null) == null ? string.Empty : prop.GetValue(item, null).ToString();
colResult += string.Format("{1} {0} = ''; ", colName, colType);
Console.WriteLine(colResult);
dataTable.Rows.Add(dataRow);
public string FirstName { get;set; }
public string LastName { get; set; }
public int Age { get; set; }
public string ParamName { get; set; }
public string ParamType { get; set; }
public object ParamValue { get; set; }
public int ParamDirection { get; set; }
public int QueryParamId { get; set; }
public string QueryId { get; set; }
public string ParamName { get; set; }
public string ParamType { get; set; }
public int ParamLength { get; set; }
public string ParamValue { get; set; }
public string ValueList { get; set; }
public bool Required { get; set; }
public int ParamOrder { get; set; }
public static class Extension
public static DataTable ToDataTable<T>(this List<T> listObject)
dynamic dynamicObject = listObject;
return ToDataTable(dynamicObject);