纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.1 KiB

3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. namespace NFine.Code
  6. {
  7. public static partial class ExtLinq
  8. {
  9. public static Expression Property(this Expression expression, string propertyName)
  10. {
  11. return Expression.Property(expression, propertyName);
  12. }
  13. /// <summary>
  14. /// 先满足第一个,再满足第二个
  15. /// </summary>
  16. /// <param name="left"></param>
  17. /// <param name="right"></param>
  18. /// <returns></returns>
  19. public static Expression AndAlso(this Expression left, Expression right)
  20. {
  21. return Expression.AndAlso(left, right);
  22. }
  23. /// <summary>
  24. /// 调用参数
  25. /// </summary>
  26. /// <param name="instance"></param>
  27. /// <param name="methodName"></param>
  28. /// <param name="arguments"></param>
  29. /// <returns></returns>
  30. public static Expression Call(this Expression instance, string methodName, params Expression[] arguments)
  31. {
  32. return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments);
  33. }
  34. /// <summary>
  35. /// 大于
  36. /// </summary>
  37. /// <param name="left"></param>
  38. /// <param name="right"></param>
  39. /// <returns></returns>
  40. public static Expression GreaterThan(this Expression left, Expression right)
  41. {
  42. return Expression.GreaterThan(left, right);
  43. }
  44. /// <summary>
  45. /// 大于等于
  46. /// </summary>
  47. /// <param name="left"></param>
  48. /// <param name="right"></param>
  49. /// <returns></returns>
  50. public static Expression GreaterThanOrEqual(this Expression left, Expression right)
  51. {
  52. return Expression.GreaterThanOrEqual(left, right);
  53. }
  54. /// <summary>
  55. /// 小于
  56. /// </summary>
  57. /// <param name="left"></param>
  58. /// <param name="right"></param>
  59. /// <returns></returns>
  60. public static Expression LessThan(this Expression left, Expression right)
  61. {
  62. return Expression.LessThan(left, right);
  63. }
  64. /// <summary>
  65. /// 小于等于
  66. /// </summary>
  67. /// <param name="left"></param>
  68. /// <param name="right"></param>
  69. /// <returns></returns>
  70. public static Expression LessThanOrEqual(this Expression left, Expression right)
  71. {
  72. return Expression.LessThanOrEqual(left, right);
  73. }
  74. public static Expression<T> ToLambda<T>(this Expression body, params ParameterExpression[] parameters)
  75. {
  76. return Expression.Lambda<T>(body, parameters);
  77. }
  78. public static Expression<Func<T, bool>> True<T>() { return param => true; }
  79. public static Expression<Func<T, bool>> False<T>() { return param => false; }
  80. public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
  81. {
  82. return first.Compose(second, Expression.AndAlso);
  83. }
  84. public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
  85. {
  86. return first.Compose(second, Expression.OrElse);
  87. }
  88. public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
  89. {
  90. var map = first.Parameters
  91. .Select((f, i) => new { f, s = second.Parameters[i] })
  92. .ToDictionary(p => p.s, p => p.f);
  93. var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
  94. return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
  95. }
  96. private class ParameterRebinder : ExpressionVisitor
  97. {
  98. readonly Dictionary<ParameterExpression, ParameterExpression> map;
  99. /// <summary>
  100. /// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
  101. /// </summary>
  102. /// <param name="map">The map.</param>
  103. ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
  104. {
  105. this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
  106. }
  107. /// <summary>
  108. /// Replaces the parameters.
  109. /// </summary>
  110. /// <param name="map">The map.</param>
  111. /// <param name="exp">The exp.</param>
  112. /// <returns>Expression</returns>
  113. public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
  114. {
  115. return new ParameterRebinder(map).Visit(exp);
  116. }
  117. protected override Expression VisitParameter(ParameterExpression p)
  118. {
  119. ParameterExpression replacement;
  120. if (map.TryGetValue(p, out replacement))
  121. {
  122. p = replacement;
  123. }
  124. return base.VisitParameter(p);
  125. }
  126. }
  127. }
  128. }