IList转换Datatable

来源:互联网 发布:php 怎么去掉html标签 编辑:程序博客网 时间:2024/04/27 21:21

  private DataTable ConvertDataTable(IList list)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        System.Reflection.PropertyInfo[]  p = list[0].GetType().GetProperties();

        foreach (System.Reflection.PropertyInfo pi in p)
        {
            dt.Columns.Add(pi.Name,Type.GetType(pi.PropertyType.ToString()));
        }

        for (int i = 0; i < list.Count; i++)
        {
            IList temp = new ArrayList();
            foreach (System.Reflection.PropertyInfo pi in p)
            {
                object obj = pi.GetValue(list[i], null);
                temp.Add(obj);
            }
            object[] o = new object[p.Length];
            for(int j = 0 ;j<temp.Count;j++)
            {
                o.SetValue(temp[j],j);
            }
            dt.LoadDataRow(o, true);
        }
        return dt;
    }

 

0 0
原创粉丝点击