123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text.Json;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Extension;
- namespace SDK.Helpers
- {
- public static class ObjectCopyConvert
- {
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="ST"></typeparam>
- /// <param name="json"></param>
- /// <param name="target"></param>
- /// <param name="filter">只修改部分指定的属性</param>
- public static void CopyToSameProperty<ST>(this JsonElement json, ST target, List<string>? filter = null)
- {
- ST? source = json.ToObject<ST>();
- PropertyInfo[] sourceProperties = source!.GetType().GetProperties();
- PropertyInfo[] targetProperties = target!.GetType().GetProperties();
- foreach (PropertyInfo sourceProperty in sourceProperties)
- {
- //数据类型相同,属性名称相同,可能存在赋值关系。
- PropertyInfo? targetProperty = targetProperties.FirstOrDefault(p => p.Name == sourceProperty.Name && p.PropertyType == sourceProperty.PropertyType);
- var data_source = sourceProperty.GetValue(source);
- if (targetProperty != null && sourceProperty.GetValue(source) != null)
- {
- // JsonElement element_source = data_source!.ToJsonString().ToObject<JsonElement>();
- //var data_target = targetProperty.GetValue(target);
- //JsonElement element_target = data_target != null ? data_target.ToJsonString().ToObject<JsonElement>() : default;
- JsonElement temp = default;
- if (json.TryGetProperty(sourceProperty.Name, out temp)
- //驼峰命名,首字母小写
- || json.TryGetProperty($"{sourceProperty.Name.Substring(0, 1).ToLower()}{sourceProperty.Name.Substring(1)}", out temp))
- {
- if (!filter.IsNotEmpty())
- {
- //只要传递了值才会运行此处
- targetProperty.SetValue(target, sourceProperty.GetValue(source));
- }
- else
- {
- if (filter!.Contains(targetProperty.Name))
- {
- targetProperty.SetValue(target, sourceProperty.GetValue(source));
- }
- }
- }
- else
- {
- // Console.WriteLine("默认值,如数字,bool类型下的默认值");
- }
- //var json= sourceProperty.GetValue(source)!.ToJsonString().ToObject<JsonElement>();
- //判断数字类型,如果前后数字相同,则不赋值,防止出现0这种。
- //判断True ,如果相同,也不赋值,防止出现
- }
- }
- }
- /// <summary>
- /// 复制类型
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <typeparam name="V"></typeparam>
- /// <param name="json"></param>
- /// <param name="target"></param>
- /// <param name="filter"></param>
- public static void CopyToSameProperty<S, T>(this JsonElement json, T target, List<string>? filter = null)
- {
- S? source = json.ToObject<S>();
- PropertyInfo[] sourceProperties = source!.GetType().GetProperties();
- PropertyInfo[] targetProperties = target!.GetType().GetProperties();
- foreach (PropertyInfo sourceProperty in sourceProperties)
- {
- //数据类型相同,属性名称相同,可能存在赋值关系。
- PropertyInfo? targetProperty = targetProperties.FirstOrDefault(p => p.Name == sourceProperty.Name && p.PropertyType==sourceProperty.PropertyType);
- var data_source = sourceProperty.GetValue(source);
- if (targetProperty != null && sourceProperty.GetValue(source) != null)
- {
- // JsonElement element_source = data_source!.ToJsonString().ToObject<JsonElement>();
- //var data_target = targetProperty.GetValue(target);
- //JsonElement element_target = data_target != null ? data_target.ToJsonString().ToObject<JsonElement>() : default;
- JsonElement temp = default;
- if (json.TryGetProperty(sourceProperty.Name, out temp)
- //驼峰命名,首字母小写
- || json.TryGetProperty($"{sourceProperty.Name.Substring(0, 1).ToLower()}{sourceProperty.Name.Substring(1)}", out temp))
- {
- if (!filter.IsNotEmpty())
- {
- //只要传递了值才会运行此处
- targetProperty.SetValue(target, sourceProperty.GetValue(source));
- }
- else
- {
- if (filter!.Contains(targetProperty.Name))
- {
- targetProperty.SetValue(target, sourceProperty.GetValue(source));
- }
- }
- }
- else
- {
- // Console.WriteLine("默认值,如数字,bool类型下的默认值");
- }
- //var json= sourceProperty.GetValue(source)!.ToJsonString().ToObject<JsonElement>();
- //判断数字类型,如果前后数字相同,则不赋值,防止出现0这种。
- //判断True ,如果相同,也不赋值,防止出现
- }
- }
- }
- }
- }
|