ObjectCopyConvert.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using HTEXLib.COMM.Helpers;
  2. using Microsoft.IdentityModel.Tokens;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.Extension;
  11. namespace SDK.Helpers
  12. {
  13. public static class ObjectCopyConvert
  14. {
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. /// <typeparam name="ST"></typeparam>
  19. /// <param name="json"></param>
  20. /// <param name="target"></param>
  21. /// <param name="filter">只修改部分指定的属性</param>
  22. public static void CopyToSameProperty<ST>(this JsonElement json, ST target, List<string>? filter = null) {
  23. ST? source = json.ToObject<ST>();
  24. PropertyInfo[] sourceProperties = source!.GetType().GetProperties();
  25. PropertyInfo[] targetProperties = target!.GetType().GetProperties();
  26. foreach (PropertyInfo sourceProperty in sourceProperties)
  27. {
  28. //数据类型相同,属性名称相同,可能存在赋值关系。
  29. PropertyInfo? targetProperty = targetProperties.FirstOrDefault(p => p.Name == sourceProperty.Name && p.PropertyType == sourceProperty.PropertyType);
  30. var data_source = sourceProperty.GetValue(source);
  31. if (targetProperty != null && sourceProperty.GetValue(source) != null)
  32. {
  33. // JsonElement element_source = data_source!.ToJsonString().ToObject<JsonElement>();
  34. //var data_target = targetProperty.GetValue(target);
  35. //JsonElement element_target = data_target != null ? data_target.ToJsonString().ToObject<JsonElement>() : default;
  36. JsonElement temp = default;
  37. if (json.TryGetProperty(sourceProperty.Name, out temp)
  38. //驼峰命名,首字母小写
  39. || json.TryGetProperty($"{sourceProperty.Name.Substring(0, 1).ToLower()}{sourceProperty.Name.Substring(1)}", out temp))
  40. {
  41. if (filter.IsEmpty())
  42. {
  43. //只要传递了值才会运行此处
  44. targetProperty.SetValue(target, sourceProperty.GetValue(source));
  45. }
  46. else
  47. {
  48. if (filter!.Contains(targetProperty.Name))
  49. {
  50. targetProperty.SetValue(target, sourceProperty.GetValue(source));
  51. }
  52. }
  53. }
  54. else
  55. {
  56. // Console.WriteLine("默认值,如数字,bool类型下的默认值");
  57. }
  58. //var json= sourceProperty.GetValue(source)!.ToJsonString().ToObject<JsonElement>();
  59. //判断数字类型,如果前后数字相同,则不赋值,防止出现0这种。
  60. //判断True ,如果相同,也不赋值,防止出现
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// 复制类型
  66. /// </summary>
  67. /// <typeparam name="T"></typeparam>
  68. /// <typeparam name="V"></typeparam>
  69. /// <param name="json"></param>
  70. /// <param name="target"></param>
  71. /// <param name="filter"></param>
  72. public static void CopyToSameProperty<S, T>(this JsonElement json, T target, List<string>? filter = null)
  73. {
  74. S? source=json.ToObject<S>();
  75. PropertyInfo[] sourceProperties = source!.GetType().GetProperties();
  76. PropertyInfo[] targetProperties = target!.GetType().GetProperties();
  77. foreach (PropertyInfo sourceProperty in sourceProperties)
  78. {
  79. //数据类型相同,属性名称相同,可能存在赋值关系。
  80. PropertyInfo? targetProperty = targetProperties.FirstOrDefault(p => p.Name == sourceProperty.Name && p.PropertyType==sourceProperty.PropertyType );
  81. var data_source = sourceProperty.GetValue(source);
  82. if (targetProperty != null && sourceProperty.GetValue(source) != null)
  83. {
  84. // JsonElement element_source = data_source!.ToJsonString().ToObject<JsonElement>();
  85. //var data_target = targetProperty.GetValue(target);
  86. //JsonElement element_target = data_target != null ? data_target.ToJsonString().ToObject<JsonElement>() : default;
  87. JsonElement temp = default;
  88. if (json.TryGetProperty(sourceProperty.Name, out temp)
  89. //驼峰命名,首字母小写
  90. || json.TryGetProperty($"{sourceProperty.Name.Substring(0, 1).ToLower()}{sourceProperty.Name.Substring(1)}", out temp))
  91. {
  92. if (filter.IsEmpty())
  93. {
  94. //只要传递了值才会运行此处
  95. targetProperty.SetValue(target, sourceProperty.GetValue(source));
  96. }
  97. else {
  98. if (filter!.Contains(targetProperty.Name))
  99. {
  100. targetProperty.SetValue(target, sourceProperty.GetValue(source));
  101. }
  102. }
  103. }
  104. else {
  105. // Console.WriteLine("默认值,如数字,bool类型下的默认值");
  106. }
  107. //var json= sourceProperty.GetValue(source)!.ToJsonString().ToObject<JsonElement>();
  108. //判断数字类型,如果前后数字相同,则不赋值,防止出现0这种。
  109. //判断True ,如果相同,也不赋值,防止出现
  110. }
  111. }
  112. }
  113. }
  114. }