| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using Infragistics.Win;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace RailLocalMeter
- {
- public static class ExtendCls
- {
- #region List转dataTable
- public static DataTable ListToDataTable<T>(this List<T> items)
- {
- DataTable dataTable = new DataTable();
- try
- {
- PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
- foreach (PropertyInfo prop in Props)
- {
- dataTable.Columns.Add(prop.Name);
- }
- foreach (T obj in items)
- {
- var values = new object[Props.Length];
- for (int i = 0; i < Props.Length; i++)
- {
- values[i] = Props[i].GetValue(obj, null);
- }
- dataTable.Rows.Add(values);
- }
- return dataTable;
- }
- catch (Exception ex)
- {
- MessageBox.Show("公共方法ListToDataTable报错!错误内容:\r\n" + ex.Message);
- return dataTable;
- }
- }
- #endregion
- #region dataTable转List
- public static List<T> TableToDataList<T>(this DataTable table)
- {
- if (table == null)
- {
- return null;
- }
- List<DataRow> rows = new List<DataRow>();
- foreach (DataRow row in table.Rows)
- {
- rows.Add(row);
- }
- return ConvertTo<T>(rows);
- }
- private static List<T> ConvertTo<T>(List<DataRow> rows)
- {
- List<T> list = null;
- if (rows != null)
- {
- list = new List<T>();
- foreach (DataRow row in rows)
- {
- T item = row.CreateItem<T>();//CreateItem<T>(row);
- list.Add(item);
- }
- }
- return list;
- }
- public static T CreateItem<T>(this DataRow row)
- {
- T obj = default(T);
- try
- {
- if (row != null)
- {
- obj = Activator.CreateInstance<T>();
- foreach (DataColumn column in row.Table.Columns)
- {
- PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
- object value = row[column.ColumnName];
- if (value.ToString() != "")
- {
- if (prop.PropertyType.FullName == typeof(string).FullName)
- {
- prop.SetValue(obj, value.ToString(), null);
- }
- else if (prop.PropertyType.FullName == typeof(DateTime?).FullName)
- {
- prop.SetValue(obj, (DateTime?)Convert.ToDateTime(value.ToString()), null);
- }
- else if (prop.PropertyType.FullName == typeof(int?).FullName)
- {
- prop.SetValue(obj, (int?)Convert.ToInt32(value.ToString()), null);
- }
- else if (prop.PropertyType.FullName == typeof(long?).FullName)
- {
- prop.SetValue(obj, (long?)Convert.ToInt32(value.ToString()), null);
- }
- else if (prop.PropertyType.FullName == typeof(double?).FullName)
- {
- prop.SetValue(obj, (double?)Convert.ToDouble(value.ToString()), null);
- }
- else if (prop.PropertyType.FullName == typeof(bool).FullName)
- {
- prop.SetValue(obj, Convert.ToBoolean(value.ToString()), null);
- }
- else if (prop.PropertyType.FullName == typeof(decimal?).FullName)
- {
- prop.SetValue(obj, Convert.ToDecimal(value.ToString()), null);
- }
- //prop.SetValue(obj, value, null);
- }
- }
- }
- }
- catch (Exception ex)
- { //You can log something here
- //throw;
- }
- return obj;
- }
- #endregion
- }
- }
|