重建表达式

我有一个表达式: Expression<Func> myExpression = (myObj, theType) => { myObj.Prop > theType };

我需要动态地将myExpression重建为Expression<Func>类型的新表达式,并将第一个表达式中的“theType”参数替换为具体值123,如:

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

我怎样才能做到这一点? 布里菲尔

使用简单的表达式替换器非常简单:

这是我为自己写的那个…支持简单的替换和多次替换。

 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 Replaces; public SimpleExpressionReplacer(Expression from, Expression to) { Replaces = new Dictionary { { from, to } }; } public SimpleExpressionReplacer(Dictionary replaces) { // Note that we should really clone from and to... But we will // ignore this! Replaces = replaces; } public SimpleExpressionReplacer(IEnumerable from, IEnumerable to) { Replaces = new Dictionary(); 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); } } 

你的TheObject

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

然后,您只需要替换表达式主体中的第二个参数,然后重建Expression<>

 public class Program { public static void Main(string[] args) { Expression> 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> myExpression2 = Expression.Lambda>(body2, myExpression.Parameters[0]); } }