StackOverflowException

StackOverflowException

本文介绍了表达式树深度限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试对深度约为 400 的 Expression> 类型的 LambdaExpression 调用 Compile() 时遇到问题.较小的值不会导致任何问题.我找不到关于这种限制的任何信息.任何人都可以澄清这一点吗?我可以增加这个限制吗?

I'm facing a problem trying to call Compile() on the LambdaExpression of type Expression<Func<MyType, bool>> which has a depth around 400. And lesser values do not cause any problems. And I can't find anything about such kind of limitation. Can anyone clarify this? Can I increase this limit?

更新:抱歉,忘了说,我收到 StackOverflowException:

upd:Sorry, forgot to mention, I'm getting StackOverflowException:

System.Core.dll 中发生类型为System.StackOverflowException"的未处理异常{无法计算表达式,因为当前线程处于堆栈溢出状态.}

推荐答案

您正在合法地遇到堆栈大小的限制(尽管我质疑 400 多项 lambda 的智慧).绕过它的最简单方法是创建一个新线程,专门用于使用更大的堆栈执行此编译,使用将堆栈大小作为参数的线程构造函数:http://msdn.microsoft.com/en-us/library/ms149581.aspx

You are legitimately running into a limit on the stack size (although I question the wisdom of a 400+ item lambda). The easiest way to bypass that is to create a new thread specifically for performing this compilation with a larger stack, using the thread constructor that takes the stack size as an argument: http://msdn.microsoft.com/en-us/library/ms149581.aspx

在该线程中调用 compile 并根据需要增加堆栈大小参数.我相信它以字节为单位,这将使默认值约为 1048576,因此从那里开始并根据需要提高它.

Call compile in that thread and increase the stack size argument as necessary. It is in bytes I believe, which would make the default about 1048576, so start there and raise it as needed.

这篇关于表达式树深度限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:51