zeroframework/Services/DeviceCenter/ZeroFramework.DeviceCenter.Infrastructure/Specifications/Extensions/ParameterReplacerVisitor.cs

34 lines
1.0 KiB
C#
Raw Permalink Normal View History

2023-12-05 09:22:48 +00:00
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;
}
}
}
}