通过输入参数来简化表达式 [英] Reduce an expression by inputting a parameter

查看:71
本文介绍了通过输入参数来简化表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个函数委托的表达式,该表达式带有许多这样的参数:

If I have an expression of a function delegate that takes a number of parameters like so:

Expression<Func<int, int, int, bool>> test = (num1, num2, num3) => num1 + num2 == num3;

有没有一种方法/如何替换其中一个值(例如,<$ c $为5) c> num1 ),然后得到与以下表达式等效的表达式:

is there a way / how can I substitute one of the values (say 5 for num1) and get the equivalent expression to:

Expression<Func<int, int, bool>> test = (num2, num3) => 5 + num2 == num3;

编辑:

也需要解决复杂类型,例如:

Also needs to resolve complex types, e.g.:

    Expression<Func<Thing, int, int>> test = (thing, num2) => thing.AnIntProp + num2;


推荐答案

我的答案是使用表达式访问者。 (感谢@ Alexei-levenkov指出来)。

My answer was to use a an expression visitor. (thanks to @Alexei-levenkov for pointing it out).

针对我的特殊情况的答案与我在问题中使用的简化示例稍有不同。但是,为了完整起见,这是我的操作方法:

The answer for my particular situation was a little different than for the simplified example I used in the question. But, for completeness, here was how I did it:

public class ResolveParameterVisitor : ExpressionVisitor
{
    private readonly ParameterExpression _param;
    private readonly object _value;

    public ResolveParameterVisitor(ParameterExpression param, object value)
    {
        _param = param;
        _value = value;
    }

    public Expression ResolveLocalValues(Expression exp)
    {
        return Visit(exp);
    }

    protected override Expression VisitParameter(ParameterExpression node)
    {
        if (node.Type == _param.Type && node.Name == _param.Name
            && node.Type.IsSimpleType())
        {
            return Expression.Constant(_value);
        }

            return base.VisitParameter(node);
    }

    protected override Expression VisitLambda<T>(Expression<T> node)
    {
        var parameters = node.Parameters.Where(p => p.Name != _param.Name && p.Type != _param.Type).ToList();
        return Expression.Lambda(Visit(node.Body), parameters);
    }
}

请注意,IsSimpleType是我从此要点。

Note that IsSimpleType is an extension I borrowed from this gist by jonothanconway.

在我的情况下,我想替换使用复杂类型。例如:

In my situation I wanted to replace use of a a complex type. e.g:

Expression<Func<Thing, int, bool>> test = (thing, num) => thing.AnIntProperty == num;

所以我重写了VisitMember方法。

So I did an override of the VisitMember method. This is still a work in progress but looks like this:

      protected override Expression VisitMember(MemberExpression m)
    {
        if (m.Expression != null
            && m.Expression.NodeType == ExpressionType.Parameter
            && m.Expression.Type == _param.Type && ((ParameterExpression)m.Expression).Name == _param.Name)
        {
            object newVal;
            if (m.Member is FieldInfo)
                newVal = ((FieldInfo)m.Member).GetValue(_value);
            else if (m.Member is PropertyInfo)
                newVal = ((PropertyInfo)m.Member).GetValue(_value, null);
            else
                newVal = null;
            return Expression.Constant(newVal);
        }

        return base.VisitMember(m);
    }

这只会解析字段或属性。下一步可能是添加对方法的支持(但由于它们本身具有参数,因此将需要更多工作...)

This will only resolve a field or property. Next step might be to add support for a method (but as they have params themselves, so that'll take some more work...)

编辑:以上成员访问者解决方案也不支持将Object本身传递给方法调用。例如(x,事物)=> x.DoSomething(thing),因此也需要进行修改。

The above Member visitor solution also wouldn't support passing an Object itself into a method call. e.g. (x, thing) => x.DoSomething(thing) so a modification would be needed to do that also.

这篇关于通过输入参数来简化表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆