FastCopy.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using static System.Linq.Expressions.Expression;
  8. namespace SqlSugar
  9. {
  10. internal static class FastCopy
  11. {
  12. static ConcurrentDictionary<string, object> copiers = new ConcurrentDictionary<string, object>();
  13. /// <summary>
  14. /// 复制两个对象同名属性值
  15. /// </summary>
  16. /// <typeparam name="S"></typeparam>
  17. /// <typeparam name="T"></typeparam>
  18. /// <param name="source">源对象</param>
  19. /// <param name="target">目标对象</param>
  20. /// <param name="copyNull">源对象属性值为null时,是否将值复制给目标对象</param>
  21. public static void Copy<S, T>(S source, T target, bool copyNull = true)
  22. {
  23. string name = string.Format("{0}_{1}_{2}", typeof(S), typeof(T), copyNull);
  24. object targetCopier;
  25. if (!copiers.TryGetValue(name, out targetCopier))
  26. {
  27. Action<S, T> copier = CreateCopier<S, T>(copyNull);
  28. copiers.TryAdd(name, copier);
  29. targetCopier = copier;
  30. }
  31. Action<S, T> action = (Action<S, T>)targetCopier;
  32. action(source, target);
  33. }
  34. /// <summary>
  35. /// 为指定的两种类型编译生成属性复制委托
  36. /// </summary>
  37. /// <typeparam name="S"></typeparam>
  38. /// <typeparam name="T"></typeparam>
  39. /// <param name="copyNull">源对象属性值为null时,是否将值复制给目标对象</param>
  40. /// <returns></returns>
  41. private static Action<S, T> CreateCopier<S, T>(bool copyNull)
  42. {
  43. ParameterExpression source = Parameter(typeof(S));
  44. ParameterExpression target = Parameter(typeof(T));
  45. var sourceProps = typeof(S).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanRead).ToList();
  46. var targetProps = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanWrite).ToList();
  47. // 查找可进行赋值的属性
  48. var copyProps = targetProps.Where(tProp => sourceProps.Where(sProp => sProp.Name == tProp.Name// 名称一致 且
  49. && (
  50. sProp.PropertyType == tProp.PropertyType// 属性类型一致 或
  51. || sProp.PropertyType.IsAssignableFrom(tProp.PropertyType) // 源属性类型 为 目标属性类型 的 子类;eg:object target = string source; 或
  52. || (tProp.PropertyType.IsValueType && sProp.PropertyType.IsValueType && // 属性为值类型且基础类型一致,但目标属性为可空类型 eg:int? num = int num;
  53. ((tProp.PropertyType.GenericTypeArguments.Length > 0 ? tProp.PropertyType.GenericTypeArguments[0] : tProp.PropertyType) == sProp.PropertyType))
  54. )).Count() > 0);
  55. List<Expression> expressionList = new List<Expression>();
  56. foreach (var prop in copyProps)
  57. {
  58. if (prop.PropertyType.IsValueType)// 属性为值类型
  59. {
  60. PropertyInfo sProp = typeof(S).GetProperty(prop.Name);
  61. PropertyInfo tProp = typeof(T).GetProperty(prop.Name);
  62. if (sProp.PropertyType == tProp.PropertyType)// 属性类型一致 eg:int num = int num; 或 int? num = int? num;
  63. {
  64. var assign = Assign(Property(target, prop.Name), Property(source, prop.Name));
  65. expressionList.Add(assign);
  66. }
  67. else if (sProp.PropertyType.GenericTypeArguments.Length <= 0 && tProp.PropertyType.GenericTypeArguments.Length > 0)// 属性类型不一致且目标属性类型为可空类型 eg:int? num = int num;
  68. {
  69. var convert = Convert(Expression.Property(source, prop.Name), tProp.PropertyType);
  70. var cvAssign = Assign(Expression.Property(target, prop.Name), convert);
  71. expressionList.Add(cvAssign);
  72. }
  73. }
  74. else// 属性为引用类型
  75. {
  76. var assign = Assign(Property(target, prop.Name), Property(source, prop.Name));// 编译生成属性赋值语句 target.{PropertyName} = source.{PropertyName};
  77. var sourcePropIsNull = Equal(Constant(null, prop.PropertyType), Property(source, prop.Name));// 判断源属性值是否为Null;编译生成 source.{PropertyName} == null
  78. var setNull = IsTrue(Constant(copyNull));// 判断是否复制Null值 编译生成 copyNull == True
  79. var setNullTest = IfThen(setNull, assign);
  80. var condition = IfThenElse(sourcePropIsNull, setNullTest, assign);
  81. /**
  82. * 编译生成
  83. * if(source.{PropertyName} == null)
  84. * {
  85. * if(setNull)
  86. * {
  87. * target.{PropertyName} = source.{PropertyName};
  88. * }
  89. * }
  90. * else
  91. * {
  92. * target.{PropertyName} = source.{PropertyName};
  93. * }
  94. */
  95. expressionList.Add(condition);
  96. }
  97. }
  98. var block = Block(expressionList.ToArray());
  99. Expression<Action<S, T>> lambda = Lambda<Action<S, T>>(block, source, target);
  100. return lambda.Compile();
  101. }
  102. internal static List<string> GetDiff<T>(T item, T trackingData) where T : class, new()
  103. {
  104. List<string> result = new List<string>();
  105. foreach (var t in typeof(T).GetProperties())
  106. {
  107. var leftvalue = t.GetValue(item);
  108. var rightvalue = t.GetValue(trackingData);
  109. if ((leftvalue == null && rightvalue != null))
  110. {
  111. result.Add(t.Name);
  112. }
  113. else if (leftvalue!=null&&!leftvalue.Equals(rightvalue))
  114. {
  115. result.Add(t.Name);
  116. }
  117. }
  118. return result;
  119. }
  120. }
  121. }