using HTEXLib.COMM.Helpers;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.Extension;
namespace SDK.Helpers
{
public static class ObjectCopyConvert
{
///
///
///
///
///
///
/// 只修改部分指定的属性
public static void CopyToSameProperty(this JsonElement json, ST target, List? filter = null) {
ST? source = json.ToObject();
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();
//var data_target = targetProperty.GetValue(target);
//JsonElement element_target = data_target != null ? data_target.ToJsonString().ToObject() : 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.IsEmpty())
{
//只要传递了值才会运行此处
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();
//判断数字类型,如果前后数字相同,则不赋值,防止出现0这种。
//判断True ,如果相同,也不赋值,防止出现
}
}
}
///
/// 复制类型
///
///
///
///
///
///
public static void CopyToSameProperty(this JsonElement json, T target, List? filter = null)
{
S? source=json.ToObject();
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();
//var data_target = targetProperty.GetValue(target);
//JsonElement element_target = data_target != null ? data_target.ToJsonString().ToObject() : 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.IsEmpty())
{
//只要传递了值才会运行此处
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();
//判断数字类型,如果前后数字相同,则不赋值,防止出现0这种。
//判断True ,如果相同,也不赋值,防止出现
}
}
}
}
}