diff --git a/Tiobon.Core.Common/Extensions/GenericTypeExtensions.cs b/Tiobon.Core.Common/Extensions/GenericTypeExtensions.cs
index 7743d10c..99c26abc 100644
--- a/Tiobon.Core.Common/Extensions/GenericTypeExtensions.cs
+++ b/Tiobon.Core.Common/Extensions/GenericTypeExtensions.cs
@@ -1,5 +1,6 @@
-using System;
-using System.Linq;
+using Newtonsoft.Json.Linq;
+using System.Linq.Expressions;
+using System.Reflection;
namespace Tiobon.Core.Common.Extensions
{
@@ -52,5 +53,322 @@ namespace Tiobon.Core.Common.Extensions
// // 判断逻辑
// bool IsTheRawGenericType(Type type) => generic == (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
// }
+
+ ///
+ /// 复制 的副本
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static T Clone(T source)
+ where T : class
+ {
+ var text = System.Text.Json.JsonSerializer.Serialize(source, typeof(T));
+ return System.Text.Json.JsonSerializer.Deserialize(text);
+ }
+
+ public static TTarget CloneTo(this object source, params object[] args)
+ where TTarget : class
+ {
+ var target = (TTarget)Activator.CreateInstance(typeof(TTarget), args);
+
+ return target.CopyFrom(source);
+ }
+
+ ///
+ /// 将 的值复制到 ,
+ /// 仅复制同名的属性或字段
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static TTarget CopyFrom(this TTarget target, TSource source)
+ where TTarget : class
+ where TSource : class
+ {
+ foreach (var member in source.GetType().GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
+ {
+ switch (member)
+ {
+ case PropertyInfo property:
+ target.PrivateSet(member.Name, property.GetValue(source));
+ break;
+ case FieldInfo field:
+ target.PrivateSet(member.Name, field.GetValue(source));
+ break;
+ default:
+ break;
+ }
+ }
+ return target;
+ }
+
+ ///
+ /// 为对象的指定属性或字段赋值
+ ///
+ ///
+ /// 对象
+ /// 属性或字段名称
+ /// 值
+ /// 当前对象
+ public static T PrivateSet(this T source, string name, object value) where T : class
+ {
+ if (source != null && !string.IsNullOrEmpty(name))
+ {
+ Type t = typeof(T);
+ var members = t.GetMember(name, MemberTypes.Property | MemberTypes.Field, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
+ foreach (var member in members)
+ {
+ switch (member)
+ {
+ case PropertyInfo property:
+ property.SetValue(source, value);
+ break;
+ case FieldInfo field:
+ field.SetValue(source, value);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ return source;
+ }
+
+ ///
+ /// 为对象的指定属性或字段赋值
+ ///
+ ///
+ ///
+ /// 对象
+ /// 选择了某个属性或字段的表达式
+ /// 值
+ /// 当前对象
+ public static T PrivateSet(this T source, Expression> expression, TKey value) where T : class
+ {
+ if (source != null && expression != null)
+ {
+ if (expression.Body is MemberExpression m && m.Member != null)
+ {
+ switch (m.Member)
+ {
+ case PropertyInfo property:
+ property.SetValue(source, value);
+ break;
+ case FieldInfo field:
+ field.SetValue(source, value);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ return source;
+ }
+
+ ///
+ /// 是否为NULL
+ ///
+ ///
+ ///
+ ///
+ public static bool IsNull(this T entity) where T : class
+ {
+ return entity == null;
+ }
+
+ ///
+ /// 获取 类型的值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static TKey GetValueFromField(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ return default;
+ return (TKey)entity.GetType().GetProperty(field).GetValue(entity);
+ }
+
+ ///
+ /// 获取object类型值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static object GetValueFromField(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ return null;
+ return typeof(T).GetProperties().FirstOrDefault(p => p.Name == field)?.GetValue(entity, null);
+ }
+
+ ///
+ /// 获取string类型值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static string GetStringValueFromField(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ return null;
+ var value = entity.GetValueFromField(field);
+ return value == null ? string.Empty : value.ToString();
+ }
+
+ ///
+ /// 获取JObject类型值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static JObject GetJsonValueFromField(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ return null;
+ var value = entity.GetValueFromField(field);
+ return value == null ? null : Newtonsoft.Json.JsonConvert.DeserializeObject(value.ToString());
+ }
+
+ ///
+ /// 获取int类型值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static int GetIntValueFromField(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ return 0;
+ var value = entity.GetValueFromField(field);
+ return value == null ? 0 : int.Parse(value.ToString());
+ }
+
+ ///
+ /// 获取double类型值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static double GetDoubleValueFromField(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ return 0;
+ var value = entity.GetValueFromField(field);
+ return value == null ? 0 : double.Parse(value.ToString());
+ }
+
+ ///
+ /// 获取DateTime类型值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static DateTime GetDateTimeValueFromFieldNotNull(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ return DateTime.MaxValue;
+ var value = entity.GetValueFromField(field);
+ if (value == null || string.IsNullOrEmpty(value.ToString()))
+ return DateTime.MaxValue;
+ return DateTime.Parse(value.ToString());
+ }
+
+ ///
+ /// 获取DateTime?类型值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static DateTime? GetDateTimeValueFromField(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ {
+ return null;
+ }
+ var value = entity.GetValueFromField(field);
+ if (value == null || string.IsNullOrEmpty(value.ToString()))
+ return null;
+ return DateTime.Parse(value.ToString());
+ }
+
+ ///
+ /// 判断是否为DateTime类型
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static bool FieldTypeIsDateTime(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ {
+ return false;
+ }
+ var t = typeof(T).GetProperties().FirstOrDefault(p => p.Name == field).PropertyType;
+ return t == typeof(DateTime) || t == typeof(DateTime?);
+ }
+
+ ///
+ /// 判断是否为数值类型
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static bool FieldTypeIsNumber(this T entity, string field) where T : class
+ {
+ if (!entity.HasField(field))
+ {
+ return false;
+ }
+ var t = typeof(T).GetProperties().FirstOrDefault(p => p.Name == field).PropertyType;
+ return t == typeof(int) || t == typeof(int?)
+ || t == typeof(double) || t == typeof(double?)
+ || t == typeof(decimal) || t == typeof(decimal?)
+ || t == typeof(long) || t == typeof(long?)
+ || t == typeof(float) || t == typeof(float?);
+ }
+
+ ///
+ /// 赋值
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static void SetValueForField(this T entity, string field, object value) where T : class
+ {
+ if (!entity.HasField(field))
+ {
+ return;
+ }
+ typeof(T).GetProperties().FirstOrDefault(p => p.Name == field).SetValue(entity, value);
+ }
+
+ ///
+ /// 是否包含该字段
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static bool HasField(this T entity, string field) where T : class
+ {
+ return typeof(T).GetProperties().Any(p => p.Name == field);
+ }
}
}
\ No newline at end of file