Rebuild Expression

牧云@^-^@ 提交于 2019-12-23 01:13:49

问题


I have an expression like: Expression<Func<TheObject, int, bool>> myExpression = (myObj, theType) => { myObj.Prop > theType };

I need to dynamically rebuild myExpression to a new expression of type Expression<Func<TheObject, bool>> and replace “theType” parameter from the first expression with a concrete value 123 like:

Expression<Func<TheObject, bool>> myNewExpression = myObj => { myObj.Prop > 123 };

How can I do that? Br Philip


回答1:


With a simple expression replacer it is quite easy:

This is the one I've written for myself... supports simple replaces and multiple replaces.

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

// A simple expression visitor to replace some nodes of an expression 
// with some other nodes. Can be used with anything, not only with
// ParameterExpression
public class SimpleExpressionReplacer : ExpressionVisitor
{
    public readonly Dictionary<Expression, Expression> Replaces;

    public SimpleExpressionReplacer(Expression from, Expression to)
    {
        Replaces = new Dictionary<Expression, Expression> { { from, to } };
    }

    public SimpleExpressionReplacer(Dictionary<Expression, Expression> replaces)
    {
        // Note that we should really clone from and to... But we will
        // ignore this!
        Replaces = replaces;
    }

    public SimpleExpressionReplacer(IEnumerable<Expression> from, IEnumerable<Expression> to)
    {
        Replaces = new Dictionary<Expression, Expression>();

        using (var enu1 = from.GetEnumerator())
        using (var enu2 = to.GetEnumerator())
        {
            while (true)
            {
                bool res1 = enu1.MoveNext();
                bool res2 = enu2.MoveNext();

                if (!res1 || !res2)
                {
                    if (!res1 && !res2)
                    {
                        break;
                    }

                    if (!res1)
                    {
                        throw new ArgumentException("from shorter");
                    }

                    throw new ArgumentException("to shorter");
                }

                Replaces.Add(enu1.Current, enu2.Current);
            }
        }
    }

    public override Expression Visit(Expression node)
    {
        Expression to;

        if (node != null && Replaces.TryGetValue(node, out to))
        {
            return base.Visit(to);
        }

        return base.Visit(node);
    }
}

Your TheObject

public class TheObject
{
    public int Prop { get; set; }
}

Then you only need to replace the 2nd parameter in the body of the expression and rebuild the Expression<>

public class Program
{
    public static void Main(string[] args)
    {
        Expression<Func<TheObject, int, bool>> myExpression = (myObj, theType) => myObj.Prop > theType;

        int value = 123;

        var body = myExpression.Body;

        var body2 = new SimpleExpressionReplacer(myExpression.Parameters[1], Expression.Constant(value)).Visit(body);

        Expression<Func<TheObject, bool>> myExpression2 = Expression.Lambda<Func<TheObject, bool>>(body2, myExpression.Parameters[0]);
    }
}


来源:https://stackoverflow.com/questions/32006815/rebuild-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!