| | 1 | | using System.Collections.ObjectModel; |
| | 2 | | using System.Linq.Expressions; |
| | 3 | |
|
| | 4 | | namespace Plainquire.Filter.Abstractions.ExpressionVisitors; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Replaces all parameters within an expression with another one. |
| | 8 | | /// Implements <see cref="ExpressionVisitor"/> |
| | 9 | | /// </summary> |
| | 10 | | /// <seealso cref="ExpressionVisitor"/> |
| | 11 | | public class ExpressionParameterReplaceVisitor : ExpressionVisitor |
| | 12 | | { |
| | 13 | | private readonly ReadOnlyCollection<ParameterExpression> _from; |
| | 14 | | private readonly ReadOnlyCollection<ParameterExpression> _to; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Initializes a new instance of the <see cref="ExpressionParameterReplaceVisitor"/> class. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="from">The parameter to replace.</param> |
| | 20 | | /// <param name="to">The parameter to inset.</param> |
| 82 | 21 | | public ExpressionParameterReplaceVisitor(ReadOnlyCollection<ParameterExpression> from, ReadOnlyCollection<ParameterE |
| | 22 | | { |
| 82 | 23 | | _from = from; |
| 82 | 24 | | _to = to; |
| 82 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <inheritdoc /> |
| | 28 | | protected override Expression VisitParameter(ParameterExpression node) |
| | 29 | | { |
| 706 | 30 | | for (var i = 0; i < _from.Count; i++) |
| 254 | 31 | | if (node == _from[i]) |
| 155 | 32 | | return _to[i]; |
| 99 | 33 | | return node; |
| | 34 | | } |
| | 35 | | } |