ColumnToClassPropertyHelper.cs 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace NX_CommonClassLibrary
  11. {
  12. public static class ColumnToClassPropertyHelper
  13. {
  14. /// <summary>
  15. /// 数据库表实体类和数据库数据的自动绑定。
  16. /// </summary>
  17. /// <typeparam name="T">限定为数据表的实体类</typeparam>
  18. /// <param name="dtCols">数据表的列集合</param>
  19. /// <param name="dr">数据表的行数据对象</param>
  20. /// <returns></returns>
  21. public static T ColumnToClassProperty<T>(DataColumnCollection dtCols, DataRow dr) where T : class, new()
  22. {
  23. T tmp = new T();
  24. CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
  25. TextInfo text = cultureInfo.TextInfo;
  26. PropertyInfo[] propertys = tmp.GetType().GetProperties();
  27. foreach (PropertyInfo property in propertys)
  28. {
  29. for (int i = 0; i < dtCols.Count; i++)
  30. {
  31. string colName = dtCols[i].ColumnName;
  32. string toLower = text.ToLower(colName);
  33. string result = text.ToTitleCase(toLower);
  34. result = result.Replace("_","");
  35. if (property.Name == result)
  36. {
  37. if (property.PropertyType.FullName == typeof(DateTime).FullName)
  38. {
  39. property.SetValue(tmp, dr[colName] == DBNull.Value ? new DateTime() : Convert.ToDateTime(dr[colName]), null);
  40. }
  41. else if (property.PropertyType.FullName == typeof(int).FullName)
  42. {
  43. property.SetValue(tmp, dr[colName] == DBNull.Value ? 0 : Convert.ToInt32(dr[colName]), null);
  44. }
  45. else if (property.PropertyType.FullName == typeof(decimal).FullName)
  46. {
  47. property.SetValue(tmp, dr[colName] == DBNull.Value ? 0 : Convert.ToDecimal(dr[colName]), null);
  48. }
  49. else if (property.PropertyType.FullName == typeof(string).FullName)
  50. {
  51. property.SetValue(tmp, dr[colName] == DBNull.Value ? null : dr[colName].ToString(), null);
  52. }
  53. else if (property.PropertyType.FullName == typeof(bool).FullName || property.PropertyType.FullName == typeof(Boolean).FullName)
  54. {
  55. //property.SetValue(tmp, dr[colName] == DBNull.Value ? false : Convert.ToBoolean(dr[colName]), null);
  56. //VS编辑器建议的简化写法。 非常巧妙!!!!!!
  57. property.SetValue(tmp, dr[colName] != DBNull.Value && Convert.ToBoolean(dr[colName]), null);
  58. }
  59. else if (property.PropertyType.BaseType.FullName == typeof(Enum).FullName)
  60. {
  61. #region 旧代码_舍弃不用
  62. // 目前不知道枚举类型应该如何转换值。
  63. // 要求数据库表中凡是枚举列,均使用int类型。而且不允许为null。
  64. // 只有这样才能保证,实体类属性的枚举和数据库表中枚举列的值才能默认转换。
  65. // ToDo:后续研究,枚举类型应该如何自动转换。目标达到:能够囊括所有异常。
  66. //if (dr[colName] == DBNull.Value)
  67. //{
  68. // property.SetValue(tmp, 0, null);
  69. //}
  70. //else
  71. //{
  72. // //property.SetValue(tmp, dr[colName], null);
  73. // property.SetValue(tmp, EnumExtensionHelper.GetEnumObj1<T>(dr[colName] ?? 0), null);
  74. //}
  75. #endregion
  76. property.SetValue(tmp, Enum.Parse(property.PropertyType, (dr[colName] ?? 0).ToString()), null);
  77. }
  78. else
  79. {
  80. property.SetValue(tmp, dr[colName], null);
  81. }
  82. break;
  83. }
  84. }
  85. }
  86. return tmp;
  87. }
  88. }
  89. }