2023-02-27 19:11:57 +01:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
|
2024-04-26 19:41:44 +02:00
|
|
|
|
namespace HostsUILib.Helpers
|
2023-02-27 19:11:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
// https://stackoverflow.com/a/22569086
|
|
|
|
|
|
public static class ExpressionExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static Expression<Func<T, bool>> And<T>(
|
|
|
|
|
|
this Expression<Func<T, bool>> expr1,
|
|
|
|
|
|
Expression<Func<T, bool>> expr2)
|
|
|
|
|
|
{
|
|
|
|
|
|
var secondBody = expr2.Body.Replace(expr2.Parameters[0], expr1.Parameters[0]);
|
|
|
|
|
|
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, secondBody), expr1.Parameters);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Expression Replace(this Expression expression, Expression searchEx, Expression replaceEx)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-16 15:51:31 +01:00
|
|
|
|
internal sealed class ReplaceVisitor : ExpressionVisitor
|
2023-02-27 19:11:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
private readonly Expression _from;
|
|
|
|
|
|
private readonly Expression _to;
|
|
|
|
|
|
|
|
|
|
|
|
public ReplaceVisitor(Expression from, Expression to)
|
|
|
|
|
|
{
|
|
|
|
|
|
_from = from;
|
|
|
|
|
|
_to = to;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Expression Visit(Expression node)
|
|
|
|
|
|
{
|
|
|
|
|
|
return node == _from ? _to : base.Visit(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|