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);
    }
    }

  • 相关阅读:
    嵌入式开发之davinci--- spi 中的时钟极性CPOL和相位CPHA
    Setting up a Single Node Cluster Hadoop on Ubuntu/Debian
    Install Java JDK JRE on Ubuntu/Debian with Apt-Get
    使用WICleanup清理Windows Installer 冗余文件
    WinSxS文件夹瘦身
    Linux时间同步+国内常用的NTP服务器地址
    How to fix Mysql table crashes
    Monitorix:一款面向Linux的轻型系统和网络监测工具
    How To Secure Apache with Let's Encrypt on Ubuntu (Free SSL)
    Latex表格太宽处理方法 (How to shorten Latex table length)
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5142696.html
Copyright © 2011-2022 走看看