ExtendCls.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Infragistics.Win;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace CarLocalMeter
  11. {
  12. public static class ExtendCls
  13. {
  14. #region List转dataTable
  15. public static DataTable ListToDataTable<T>(this List<T> items)
  16. {
  17. DataTable dataTable = new DataTable();
  18. try
  19. {
  20. PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  21. foreach (PropertyInfo prop in Props)
  22. {
  23. dataTable.Columns.Add(prop.Name);
  24. }
  25. foreach (T obj in items)
  26. {
  27. var values = new object[Props.Length];
  28. for (int i = 0; i < Props.Length; i++)
  29. {
  30. values[i] = Props[i].GetValue(obj, null);
  31. }
  32. dataTable.Rows.Add(values);
  33. }
  34. return dataTable;
  35. }
  36. catch (Exception ex)
  37. {
  38. MessageBox.Show("公共方法ListToDataTable报错!错误内容:\r\n" + ex.Message);
  39. return dataTable;
  40. }
  41. }
  42. #endregion
  43. #region dataTable转List
  44. public static List<T> TableToDataList<T>(this DataTable table)
  45. {
  46. if (table == null)
  47. {
  48. return null;
  49. }
  50. List<DataRow> rows = new List<DataRow>();
  51. foreach (DataRow row in table.Rows)
  52. {
  53. rows.Add(row);
  54. }
  55. return ConvertTo<T>(rows);
  56. }
  57. private static List<T> ConvertTo<T>(List<DataRow> rows)
  58. {
  59. List<T> list = null;
  60. if (rows != null)
  61. {
  62. list = new List<T>();
  63. foreach (DataRow row in rows)
  64. {
  65. T item = row.CreateItem<T>();//CreateItem<T>(row);
  66. list.Add(item);
  67. }
  68. }
  69. return list;
  70. }
  71. public static T CreateItem<T>(this DataRow row)
  72. {
  73. T obj = default(T);
  74. try
  75. {
  76. if (row != null)
  77. {
  78. obj = Activator.CreateInstance<T>();
  79. foreach (DataColumn column in row.Table.Columns)
  80. {
  81. PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
  82. object value = row[column.ColumnName];
  83. if (value.ToString() != "")
  84. {
  85. if (prop.PropertyType.FullName == typeof(string).FullName)
  86. {
  87. prop.SetValue(obj, value.ToString(), null);
  88. }
  89. else if (prop.PropertyType.FullName == typeof(DateTime?).FullName)
  90. {
  91. prop.SetValue(obj, (DateTime?)Convert.ToDateTime(value.ToString()), null);
  92. }
  93. else if (prop.PropertyType.FullName == typeof(int?).FullName)
  94. {
  95. prop.SetValue(obj, (int?)Convert.ToInt32(value.ToString()), null);
  96. }
  97. else if (prop.PropertyType.FullName == typeof(long?).FullName)
  98. {
  99. prop.SetValue(obj, (long?)Convert.ToInt32(value.ToString()), null);
  100. }
  101. else if (prop.PropertyType.FullName == typeof(double?).FullName)
  102. {
  103. prop.SetValue(obj, (double?)Convert.ToDouble(value.ToString()), null);
  104. }
  105. else if (prop.PropertyType.FullName == typeof(bool).FullName)
  106. {
  107. prop.SetValue(obj, Convert.ToBoolean(value.ToString()), null);
  108. }
  109. else if (prop.PropertyType.FullName == typeof(decimal?).FullName)
  110. {
  111. prop.SetValue(obj, Convert.ToDecimal(value.ToString()), null);
  112. }
  113. //prop.SetValue(obj, value, null);
  114. }
  115. }
  116. }
  117. }
  118. catch (Exception ex)
  119. { //You can log something here
  120. //throw;
  121. }
  122. return obj;
  123. }
  124. #endregion
  125. }
  126. }