zoukankan      html  css  js  c++  java
  • 条件表达式工具类

    internal class ParameterReplacer : ExpressionVisitor
    {
    public ParameterReplacer(ParameterExpression paramExpr)
    {
    this.ParameterExpression = paramExpr;
    }

    public ParameterExpression ParameterExpression { get; private set; }

    public Expression Replace(Expression expr)
    {
    return this.Visit(expr);
    }

    protected override Expression VisitParameter(ParameterExpression p)
    {
    return this.ParameterExpression;
    }
    }

    /// <summary>
    /// 条件表达式工具类
    /// </summary>
    public static class ExpressionPredicate
    {

    const string DEFAULT="candidate";
    /// <summary>
    /// 创建表达式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static Expression<Func<T, bool>> Create<T>() { return f => true; }

    /// <summary>
    /// True表达式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static Expression<Func<T, bool>> True<T>() { return f => true; }

    /// <summary>
    /// False表达式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    /// <summary>
    /// Not操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="expr"></param>
    /// <returns></returns>
    public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expr)
    {
    var candidateExpr = expr.Parameters[0];
    var body = Expression.Not(expr.Body);

    return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
    }

    /// <summary>
    /// And操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="exp_left"></param>
    /// <param name="exp_right"></param>
    /// <returns></returns>
    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> exp_left, Expression<Func<T, bool>> exp_right)
    {
    var candidateExpr = Expression.Parameter(typeof(T), DEFAULT);
    var parameterReplacer = new ParameterReplacer(candidateExpr);

    var left = parameterReplacer.Replace(exp_left.Body);
    var right = parameterReplacer.Replace(exp_right.Body);
    var body = Expression.And(left, right);

    return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
    }

    /// <summary>
    /// Or操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="exp_left"></param>
    /// <param name="exp_right"></param>
    /// <returns></returns>
    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> exp_left, Expression<Func<T, bool>> exp_right)
    {
    var candidateExpr = Expression.Parameter(typeof(T), DEFAULT);
    var parameterReplacer = new ParameterReplacer(candidateExpr);

    var left = parameterReplacer.Replace(exp_left.Body);
    var right = parameterReplacer.Replace(exp_right.Body);
    var body = Expression.Or(left, right);

    return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
    }
    }

    或者

    public static class PredicateExtensions
    {
    public static Expression<Func<T, bool>> True<T>() { return f => true; }
    public static Expression<Func<T, bool>> False<T>() { return f => false; }
    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
    {
    var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());
    return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression), expression1.Parameters);
    }
    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
    {
    var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());
    return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);
    }
    }

  • 相关阅读:
    构建之法阅读笔记07
    7-第一阶段SCRUM冲刺
    第一阶段个人冲刺博客第十天
    第一阶段个人冲刺博客第九天
    第九周学习进度博客
    java项目(学习和研究)
    让计算机干活
    os基础
    树和图的一些算法
    java代码理解
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5142696.html
Copyright © 2011-2022 走看看