ToModeExtension.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Common.Models;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Reflection;
  5. namespace Common
  6. {
  7. public static class ToModeExtension
  8. {
  9. public static T ToModel<T>(this T model, T data)
  10. where T : class, new()
  11. {
  12. if (model == null)
  13. return null;
  14. if (data == null)
  15. return model;
  16. PropertyInfo[] propertyInfos = data.GetType().GetProperties();//获取实体下所有字段的属性
  17. PropertyInfo[] dtoproperty = model.GetType().GetProperties();
  18. for (int i = 0; i < propertyInfos.Length; i++)
  19. {
  20. for (int j = 0; j < dtoproperty.Length; j++)
  21. {
  22. if (propertyInfos[i].Name == dtoproperty[j].Name)
  23. {
  24. var ss = (TypeEnum)Enum.Parse(typeof(TypeEnum), propertyInfos[i].PropertyType.Name);
  25. var value = propertyInfos[i].GetValue(data);//获取该属性字段对应的值
  26. if (value == null)
  27. break;
  28. switch (ss)
  29. {
  30. case TypeEnum.String:
  31. if(!string.IsNullOrEmpty(value.ToString()))
  32. dtoproperty[j].SetValue(model, value);
  33. break;
  34. case TypeEnum.Int32:
  35. if (propertyInfos[i].Name == "id")
  36. {
  37. if (Convert.ToInt32(value) > 0)
  38. dtoproperty[j].SetValue(model, value);
  39. }
  40. else {
  41. if (Convert.ToInt32(value) >= 0)
  42. dtoproperty[j].SetValue(model, value);
  43. }
  44. break;
  45. case TypeEnum.Boolean:
  46. if (Convert.ToBoolean(value) == true)
  47. dtoproperty[j].SetValue(model, value);
  48. break;
  49. case TypeEnum.DateTime:
  50. if (Convert.ToDateTime(value) > Convert.ToDateTime("1949/01/01 00:00:00"))
  51. dtoproperty[j].SetValue(model, value);
  52. break;
  53. case TypeEnum.Double:
  54. if (Convert.ToDouble(value) >= 0)
  55. dtoproperty[j].SetValue(model, value);
  56. break;
  57. case TypeEnum.Decimal:
  58. if (Convert.ToDecimal(value) >= 0)
  59. dtoproperty[j].SetValue(model, value);
  60. break;
  61. default:
  62. if (!string.IsNullOrEmpty(value.ToString()))
  63. dtoproperty[j].SetValue(model, value);
  64. break;
  65. }
  66. break;
  67. }
  68. }
  69. }
  70. return model;
  71. }
  72. public static T ToEntity<T>(this object model)
  73. where T : class, new()
  74. {
  75. try
  76. {
  77. if (model == null)
  78. return null;
  79. T data = new T();
  80. Type t = typeof(T);
  81. PropertyInfo[] propertyInfos = t.GetProperties();//获取实体下所有字段的属性
  82. PropertyInfo[] dtoproperty = model.GetType().GetProperties();
  83. for (int j = 0; j < dtoproperty.Length; j++)
  84. {
  85. for (int i = 0; i < propertyInfos.Length; i++)
  86. {
  87. if (propertyInfos[i].Name == dtoproperty[j].Name)
  88. {
  89. var value = dtoproperty[j].GetValue(model);//获取该属性字段对应的值
  90. propertyInfos[i].SetValue(data, value);
  91. }
  92. }
  93. }
  94. return data;
  95. }
  96. catch (Exception e)
  97. {
  98. return null;
  99. }
  100. }
  101. #region object where
  102. public static string objectwhere(object parm)
  103. {
  104. string where = " n1>-1 ";
  105. if (parm != null)
  106. {
  107. foreach (PropertyInfo item in parm.GetType().GetProperties())
  108. {
  109. where += $" and {item.Name}=@{item.Name} ";
  110. }
  111. if (!string.IsNullOrEmpty(where))
  112. return " where " + where;
  113. }
  114. return " where " + where;
  115. }
  116. #endregion
  117. }
  118. }