C# List转换成DataTable(c反应蛋白高说明什么)

网友投稿 567 2022-09-17

C# List转换成DataTable(c反应蛋白高说明什么)

C# List转换成DataTable(c反应蛋白高说明什么)

///

/// C# List转换成DataTable

///

///

///

public static System.Data.DataTable ListToDataTable(IList list)

{

System.Data.DataTable result = new System.Data.DataTable();

if (list.Count > 0)

{

PropertyInfo[] propertys = list[0].GetType().GetProperties();

foreach (PropertyInfo pi in propertys)

{

//获取类型

Type colType = pi.PropertyType;

//当类型为Nullable<>时

if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))

{

colType = colType.GetGenericArguments()[0];

}

result.Columns.Add(pi.Name, colType);

}

for (int i = 0; i < list.Count; i++)

{

ArrayList tempList = new ArrayList();

foreach (PropertyInfo pi in propertys)

{

object obj = pi.GetValue(list[i], null);

tempList.Add(obj);

}

object[] array = tempList.ToArray();

result.LoadDataRow(array, true);

}

}

return result;

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:openstack changePassword
下一篇:不使用CHKDSK,手动重置或清除Windows中的 dirty bit (脏位)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~