ObjectCopyConvert.cs 5.8 KB

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