List<>对象转换成dataTable
private DataTable changeDataTable(object lst模块)
        {
            DataTable dt = new DataTable();
            IList list = (IList)lst模块;

            // 通过使用反射来获取列表中队形的属性

            BindingFlags bf = BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty;
            PropertyInfo[] props = list[0].GetType().GetProperties();
            foreach (PropertyInfo pi in props)
                dt.Columns.Add(pi.Name,Type.GetType(pi.PropertyType.FullName));

            foreach (object obj in list)
            {
                DataRow dr = dt.NewRow();
                foreach (PropertyInfo pi in props)
                {
                    object result = obj.GetType().InvokeMember(pi.Name, bf, null, obj, null);
                   // d.Add(result);
                    dr[pi.Name] = result;  
                }
               dt.Rows.Add(dr);
            }
            return dt;
        }

相关文章:

  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
  • 2022-02-01
  • 2022-02-18
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
猜你喜欢
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2021-06-13
相关资源
相似解决方案