编译需要参数的表达式 [英] Compile expression that requires a parameter
本文介绍了编译需要参数的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,我敢肯定这很简单,但是我有一个重要的时刻.
Ok, I am sure this is simple, but I am having a senior moment.
我有一个简单的BinaryExpression(greaterthan),左侧是ParameterExpression,右侧是ConstantExpression,我想将此表达式编译为可以调用并将参数传递给...的函子.
I have a simple BinaryExpression (greaterthan) the left side is a ParameterExpression and the right side is a ConstantExpression I want to compile this expression to a func that I can call and pass a parameter to...
var func = ...something with my exp....
bool result = func(myValue);
多亏了哈桑,我修改了他对我的需求的答案...
Thanks to Hasan, I modified his answer to my needs...
var func = Expression.Lambda<Func<int,bool>>(myExpr, (ParameterExpression)myExpr.left).Compile();
推荐答案
var param = Expression.Parameter(typeof(int));
var value = Expression.Constant(3);
var body = Expression.GreaterThan(param, value);
var checkValue = Expression.Lambda<Func<int, bool>>(body, param).Compile();
Console.WriteLine(checkValue(4));
Console.WriteLine(checkValue(2));
这篇关于编译需要参数的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文