小编sve*_*ven的帖子

具有返回值的动态Linq表达式

我需要创建一个动态的linq表达式,我开始使用许多示例.我测试了一些和一些工作而一些没有.在这种情况下,我想创建一个看起来像这样的方法:

public bool Check(int intvar)
{
   if ( i > 2 )
     return true;
   else
     return false;
}
Run Code Online (Sandbox Code Playgroud)

现在我写了以下内容:

LabelTarget returnTarget = Expression.Label("label");
ParameterExpression para = Expression.Parameter(typeof(int), "intvalue");
Expression test = Expression.GreaterThan(para, Expression.Constant(5));
Expression iftrue = Expression.Return(returnTarget, Expression.Constant(true));
Expression iffalse = Expression.Return(returnTarget,                   Expression.Constant(false));
Expression.IfThenElse(test, iftrue, iffalse);

this.TheExpression = Expression.IfThenElse(test, iftrue, iffalse);
Expression.Lambda<Action<int>>(
this.TheExpression,
new ParameterExpression[] { para }
).Compile()(5);
Run Code Online (Sandbox Code Playgroud)

现在它抛出InvalidOperationException:

无法跳转到标签"标签"`

怎么了 ?我只需要返回true或false.

c# expression-trees

11
推荐指数
1
解决办法
7797
查看次数

标签 统计

c# ×1

expression-trees ×1