4

作为 funcletizer 的一部分,我希望用它们的评估常量替换不包含参数的 c# 表达式:

double d = 100.0;

Expression<Func<double, double>> ex1 = x => -x;
Expression<Func<double>> ex2 = () => -d;

Expression result;
result = Funcletize(ex1); // should return ex1 unmodified
result = Funcletize(ex2); // should return Expression.Constant(-100.0)

我知道我可以通过将表达式包装在 lambda 表达式中并调用它来评估表达式:

object result = Expression.Lambda(ex2).Compile().DynamicInvoke(); 
// result == -100

当表达式包含未绑定的参数时,ex1如上所述,这当然会失败,抛出 InvalidOperationException,因为我没有提供任何参数。如何检查表达式是否包含此类参数?

我当前的解决方案涉及 try{} catch(InvoalidOperationException),但这似乎是一种非常不雅且容易出错的方式:

// this works; by catching InvalidOperationException
public static Expression Funcletize(Expression ex)
{
    try
    {
        // Compile() will throw InvalidOperationException, 
        // if the expression contains unbound parameters
        var lambda = Expression.Lambda(ex).Compile(); 

        Object value = lambda.DynamicInvoke();
        return Expression.Constant(value, ex.Type);
    }
    catch  (InvalidOperationException)
    {
        return ex;
    }
}
4

2 回答 2

5

当然,大多数事情都是可能的。这里有两个不同的事情在起作用:

  • 通过跟踪表达式中看到的参数来删除未使用的参数
  • 评估和内联捕获的变量(强调:这是一个语义变化)——我们通过尝试识别 field->[field->]...field-> 模式来做到这一点(尽管显示的代码实际上可能在这里执行误报在某些情况下)

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
static class Program
{
    static void Main()
    {
        double d = 100;

        Expression<Func<double, double>> ex1 = x => -x;
        Expression<Func<double>> ex2 = () => -d;

        var result1 = Demungify(ex1); // (x) => -x
        var result2 = Demungify(ex2); // () => -100
    }
    public static LambdaExpression Demungify(LambdaExpression ex)
    {
        var visitor = new Demungifier();
        var newBody = visitor.Visit(ex.Body);
        var args = ex.Parameters.Where(visitor.WasSeen).ToArray();
        var lambda = Expression.Lambda(newBody, args);
        if (!args.Any() && !(lambda.Body is ConstantExpression))
        {
            // evaluate that!
            object result = lambda.Compile().DynamicInvoke();
            lambda = Expression.Lambda(Expression.Constant(result, newBody.Type));    
        }
        return lambda;
    }
    class Demungifier : ExpressionVisitor
    {
        readonly HashSet<ParameterExpression> parameters = new HashSet<ParameterExpression>();

        public bool WasSeen(ParameterExpression param)
        {
            return parameters.Contains(param);
        }
        protected override Expression VisitParameter(ParameterExpression node)
        {
            parameters.Add(node);
            return base.VisitParameter(node);
        }
        protected override Expression VisitMember(MemberExpression node)
        {
            object value;
            if(TryEvaluate(node, out value))
            {
                return Expression.Constant(value, ((FieldInfo)node.Member).FieldType);
            }
            return base.VisitMember(node);
        }
        bool TryEvaluate(Expression expression, out object value)
        {
            if(expression == null)
            {
                value = null;
                return true;
            }
            if(expression.NodeType == ExpressionType.Constant)
            {
                value = ((ConstantExpression)expression).Value;
                return true;
            }
            // captured variables are always fields, potentially of fields of fields
            // eventually terminating in a ConstantExpression that is the capture-context
            MemberExpression member;
            if(expression.NodeType == ExpressionType.MemberAccess
                && (member= (MemberExpression)expression).Member.MemberType == System.Reflection.MemberTypes.Field)
            {
                object target;
                if(TryEvaluate(member.Expression, out target))
                {
                    value = ((FieldInfo)member.Member).GetValue(target);
                    return true;
                }
            }
            value = null;
            return false;
        }

    }
}
于 2013-09-10T11:02:06.310 回答
1

在这种情况下,您可以将其Expression转换为LambdaExpression并查看它是否具有参数。

public static Expression Funcletize(Expression ex)
{
   var l = ex as LambdaExpression;

    if (l != null && l.Parameters.Count == 0)
    {
        var lambda = Expression.Lambda(ex).Compile();
        Object value = lambda.DynamicInvoke();
        return Expression.Constant(value, ex.Type);
    }

    return ex;
}
于 2013-09-10T11:10:19.933 回答