ParameterRebinder.cs 1017 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6. namespace Bowin.Common.Linq
  7. {
  8. public sealed class ParameterRebinder : ExpressionVisitor
  9. {
  10. private readonly Dictionary<ParameterExpression, ParameterExpression> map;
  11. public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
  12. {
  13. this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
  14. }
  15. public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
  16. {
  17. return new ParameterRebinder(map).Visit(exp);
  18. }
  19. protected override Expression VisitParameter(ParameterExpression p)
  20. {
  21. ParameterExpression expression;
  22. if (this.map.TryGetValue(p, out expression))
  23. {
  24. p = expression;
  25. }
  26. return base.VisitParameter(p);
  27. }
  28. }
  29. }