构建动态表达式树时出现问题

发布于 2024-09-17 22:47:43 字数 3658 浏览 2 评论 0原文

我正在尝试构建一个动态表达式树,如下所示:

 Func<IEnumerable<int>, int, bool> dividesectionmethod = (x, y) =>
            {
                int nos1 = 0;
                int nos2 = 0;
                foreach (int i in x)
                {
                    if (i <= y)
                        nos1++;
                    else
                        nos2++;
                }
                return nos1 > nos2;
            };

为此,我正在使用:

 ParameterExpression enumerableExpression = Expression.Parameter(typeof(IEnumerable<int>), "x");
            ParameterExpression intexpression = Expression.Parameter(typeof(int), "y");

            ParameterExpression localvarnos1 = Expression.Variable(typeof(int), "nos1");
            ParameterExpression localvarnos2 = Expression.Variable(typeof(int), "nos2");
            ConstantExpression zeroConstantintval = Expression.Constant(0);
            BinaryExpression bexplocalnos1 = Expression.Assign(localvarnos1, zeroConstantintval);
            BinaryExpression bexplocalnos2 = Expression.Assign(localvarnos2, zeroConstantintval);

            //As Expression does not support Foreach we need to get Enumerator before doing loop

            ParameterExpression enumerator = Expression.Variable(typeof(IEnumerator<int>), "enumerator");
            BinaryExpression assignenumerator = Expression.Assign(enumerator, Expression.Call(enumerableExpression, typeof(IEnumerable<int>).GetMethod("GetEnumerator")));


            var currentelement = Expression.Parameter(typeof(int), "i");
            var callCurrent = Expression.Assign(currentelement, Expression.Property(enumerator, "Current"));

            BinaryExpression firstlessequalsecond = Expression.LessThanOrEqual(currentelement, intexpression);

            MethodCallExpression movenext = Expression.Call(enumerator, typeof(IEnumerator).GetMethod("MoveNext"));

            LabelTarget looplabel = Expression.Label("looplabel");
            LabelTarget returnLabel = Expression.Label(typeof(bool), "retval");

            BlockExpression block = Expression.Block(enumerableExpression, intexpression, localvarnos1, localvarnos2,
                bexplocalnos1, bexplocalnos2, Expression.Loop(Expression.IfThenElse(
                Expression.NotEqual(movenext, Expression.Constant(false)),
                Expression.IfThenElse(firstlessequalsecond, Expression.Increment(localvarnos1), Expression.Increment(localvarnos2)),Expression.Break(looplabel)), looplabel),
                Expression.Return(returnLabel, Expression.LessThan(localvarnos1, localvarnos2)));

            Expression<Func<IEnumerable<int>, int, bool>> lambda = Expression.Lambda<Func<IEnumerable<int>, int, bool>>(block, Expression.Parameter(typeof(IEnumerable<int>), "x"),
                Expression.Parameter(typeof(int), "y"));


            Func<IEnumerable<int>, int, bool> mymethod = lambda.Compile();

但问题是 Expression.Lambda 抛出异常:

Expression of type 'System.Void' cannot be used for return type 'System.Boolean'

我不知道问题所在,因为我的块似乎没问题:

.Block() {
    $x;
    $y;
    $nos1;
    $nos2;
    $nos1 = 0;
    $nos2 = 0;
    .Loop  {
        .If (.Call $enumerator.MoveNext() != False) {
            .If ($i <= $y) {
                .Increment($nos1)
            } .Else {
                .Increment($nos2)
            }
        } .Else {
            .Break looplabel { }
        }
    }
    .LabelTarget looplabel:;
    .Return retval { $nos1 < $nos2 }
}

请让我知道问题可能是什么是。

I am trying to build a dynamic Expression Tree like below :

 Func<IEnumerable<int>, int, bool> dividesectionmethod = (x, y) =>
            {
                int nos1 = 0;
                int nos2 = 0;
                foreach (int i in x)
                {
                    if (i <= y)
                        nos1++;
                    else
                        nos2++;
                }
                return nos1 > nos2;
            };

For that I am using :

 ParameterExpression enumerableExpression = Expression.Parameter(typeof(IEnumerable<int>), "x");
            ParameterExpression intexpression = Expression.Parameter(typeof(int), "y");

            ParameterExpression localvarnos1 = Expression.Variable(typeof(int), "nos1");
            ParameterExpression localvarnos2 = Expression.Variable(typeof(int), "nos2");
            ConstantExpression zeroConstantintval = Expression.Constant(0);
            BinaryExpression bexplocalnos1 = Expression.Assign(localvarnos1, zeroConstantintval);
            BinaryExpression bexplocalnos2 = Expression.Assign(localvarnos2, zeroConstantintval);

            //As Expression does not support Foreach we need to get Enumerator before doing loop

            ParameterExpression enumerator = Expression.Variable(typeof(IEnumerator<int>), "enumerator");
            BinaryExpression assignenumerator = Expression.Assign(enumerator, Expression.Call(enumerableExpression, typeof(IEnumerable<int>).GetMethod("GetEnumerator")));


            var currentelement = Expression.Parameter(typeof(int), "i");
            var callCurrent = Expression.Assign(currentelement, Expression.Property(enumerator, "Current"));

            BinaryExpression firstlessequalsecond = Expression.LessThanOrEqual(currentelement, intexpression);

            MethodCallExpression movenext = Expression.Call(enumerator, typeof(IEnumerator).GetMethod("MoveNext"));

            LabelTarget looplabel = Expression.Label("looplabel");
            LabelTarget returnLabel = Expression.Label(typeof(bool), "retval");

            BlockExpression block = Expression.Block(enumerableExpression, intexpression, localvarnos1, localvarnos2,
                bexplocalnos1, bexplocalnos2, Expression.Loop(Expression.IfThenElse(
                Expression.NotEqual(movenext, Expression.Constant(false)),
                Expression.IfThenElse(firstlessequalsecond, Expression.Increment(localvarnos1), Expression.Increment(localvarnos2)),Expression.Break(looplabel)), looplabel),
                Expression.Return(returnLabel, Expression.LessThan(localvarnos1, localvarnos2)));

            Expression<Func<IEnumerable<int>, int, bool>> lambda = Expression.Lambda<Func<IEnumerable<int>, int, bool>>(block, Expression.Parameter(typeof(IEnumerable<int>), "x"),
                Expression.Parameter(typeof(int), "y"));


            Func<IEnumerable<int>, int, bool> mymethod = lambda.Compile();

But the problem is Expression.Lambda throws an exception :

Expression of type 'System.Void' cannot be used for return type 'System.Boolean'

I dont know the issue as my block seems to be Ok:

.Block() {
    $x;
    $y;
    $nos1;
    $nos2;
    $nos1 = 0;
    $nos2 = 0;
    .Loop  {
        .If (.Call $enumerator.MoveNext() != False) {
            .If ($i <= $y) {
                .Increment($nos1)
            } .Else {
                .Increment($nos2)
            }
        } .Else {
            .Break looplabel { }
        }
    }
    .LabelTarget looplabel:;
    .Return retval { $nos1 < $nos2 }
}

Please let me know what the problem could be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ人海孤独症 2024-09-24 22:47:43

BlockExpression 的值只是堵塞。无需包含 ReturnExpression,只需让最后一个表达式成为您想要返回的值即可。

此外,您还需要将块的 ParameterExpressions 声明为 Expression.Block 方法的单独参数。您已将它们包含在表达式列表中,这将导致它们被计算为表达式,但不会在块中声明它们。

此外, Expression.Increment ”不会更改对象的值传递给它”,因此您需要将增量表达式包装在赋值表达式中。

BlockExpression block = Expression.Block(
    new ParameterExpression[] { 
        localvarnos1, localvarnos2, enumerator, currentelement },
    bexplocalnos1, 
    bexplocalnos2, 
    assignenumerator, 
    Expression.Loop(
        Expression.IfThenElse(
            Expression.NotEqual(movenext, Expression.Constant(false)),
            Expression.Block(
                callCurrent, 
                Expression.IfThenElse(
                    firstlessequalsecond, 
                    Expression.Assign(
                        localvarnos1, 
                        Expression.Increment(localvarnos1)), 
                    Expression.Assign(
                        localvarnos2, 
                        Expression.Increment(localvarnos2)))),
            Expression.Break(looplabel)), 
        looplabel),
    Expression.LessThan(localvarnos1, localvarnos2));

Expression<Func<IEnumerable<int>, int, bool>> lambda = 
    Expression.Lambda<Func<IEnumerable<int>, int, bool>>(
        block,
        enumerableExpression,
        intexpression);

The value of a BlockExpression is just the value of the last expression in the block. Rather than including a ReturnExpression, just let the last expression be the value you want to return.

Also, you need to declare the ParameterExpressions for the block as a separate argument to the Expression.Block method. You have included them in the list of expressions, which will cause them to be evaluated as expressions but will not declare them in the block.

Also, Expression.Increment "does not change the value of the object that is passed to it", so you will need to wrap your increment expressions in assignment expressions.

BlockExpression block = Expression.Block(
    new ParameterExpression[] { 
        localvarnos1, localvarnos2, enumerator, currentelement },
    bexplocalnos1, 
    bexplocalnos2, 
    assignenumerator, 
    Expression.Loop(
        Expression.IfThenElse(
            Expression.NotEqual(movenext, Expression.Constant(false)),
            Expression.Block(
                callCurrent, 
                Expression.IfThenElse(
                    firstlessequalsecond, 
                    Expression.Assign(
                        localvarnos1, 
                        Expression.Increment(localvarnos1)), 
                    Expression.Assign(
                        localvarnos2, 
                        Expression.Increment(localvarnos2)))),
            Expression.Break(looplabel)), 
        looplabel),
    Expression.LessThan(localvarnos1, localvarnos2));

Expression<Func<IEnumerable<int>, int, bool>> lambda = 
    Expression.Lambda<Func<IEnumerable<int>, int, bool>>(
        block,
        enumerableExpression,
        intexpression);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文