34 lines
1.0 KiB
C#
34 lines
1.0 KiB
C#
|
using System.Linq.Expressions;
|
|||
|
|
|||
|
namespace ZeroFramework.DeviceCenter.Infrastructure.Specifications.Extensions
|
|||
|
{
|
|||
|
internal class ParameterReplacerVisitor : ExpressionVisitor
|
|||
|
{
|
|||
|
private readonly Expression newExpression;
|
|||
|
|
|||
|
private readonly ParameterExpression oldParameter;
|
|||
|
|
|||
|
private ParameterReplacerVisitor(ParameterExpression oldParameter, Expression newExpression)
|
|||
|
{
|
|||
|
this.oldParameter = oldParameter;
|
|||
|
this.newExpression = newExpression;
|
|||
|
}
|
|||
|
|
|||
|
internal static Expression Replace(Expression expression, ParameterExpression oldParameter, Expression newExpression)
|
|||
|
{
|
|||
|
return new ParameterReplacerVisitor(oldParameter, newExpression).Visit(expression);
|
|||
|
}
|
|||
|
|
|||
|
protected override Expression VisitParameter(ParameterExpression p)
|
|||
|
{
|
|||
|
if (p == oldParameter)
|
|||
|
{
|
|||
|
return newExpression;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return p;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|