1 DataTable dt = new DataTable(); 2 if (_list != null) 3 { 4 //通过反射获取list中的字段 5 System.Reflection.PropertyInfo[] p = _list[0].GetType().GetProperties(); 6 foreach (System.Reflection.PropertyInfo pi in p) 7 { 8 dt.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString())); 9 } 10 for (int i = 0; i < _list.Count; i++) 11 { 12 IList TempList = new ArrayList(); 13 //将IList中的一条记录写入ArrayList 14 foreach (System.Reflection.PropertyInfo pi in p) 15 { 16 object oo = pi.GetValue(_list[i], null); 17 TempList.Add(oo); 18 } 19 object[] itm = new object[p.Length]; 20 for (int j = 0; j < TempList.Count; j++) 21 { 22 itm.SetValue(TempList[j], j); 23 } 24 dt.LoadDataRow(itm, true); 25 } 26 }