调试 .NET 动态方法
我们在我们的系统中非常广泛地使用 LINQ。 特别是 LINQ 到对象。 因此,在某些地方,我们最终会在内存中使用一些巨大的表达式构建 LINQ 查询。 当表达式中存在一些错误时,问题就会出现。 因此,我们得到 NullReferenceException,并且堆栈跟踪无法引导我们(到达 [轻量级函数])。 异常是在 LINQ 生成的动态方法内引发的。
有没有简单的方法来调试这种动态方法? 还是我必须牺牲自己来学习WinDBG? :-)
We are using LINQ very widely in our system. Particularly LINQ-to-objects. So in some places we end up having a LINQ query in memory build up from some huge expressions. The problem comes when there's some bug in the expressions. So we get NullReferenceException and the stack trace leads us nowhere (to [Lightweight Function]). The exception was thrown inside the dynamic method generated by LINQ.
Is there any easy way to debug such dynamic methods? Or do I have to sacrifice myself to learning WinDBG? :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在构建自己的表达式并编译它们,或者使用 AsQueryable,那么是的; LINQ 生成的方法调试起来会非常痛苦。
您可以通过使用实际方法的小片段来减轻一些痛苦 - 至少在堆栈跟踪中会显示一些有用的东西...
另一个考虑因素是:如果您可以菊花链,而不是使用一个巨大的表达式事情多一点,您可能会更清楚(从堆栈跟踪)它失败的地方。 缺点是性能 -Where(foo).Where(bar) 是两个委托调用,where-asWhere(foo && bar) 可以是一个。
一种选择可能是交换扩展方法的调试版本; 不幸的是,这有点不方便,因为
IQueryable
和Queryable
位于同一名称空间中......不过,这是可行的......首先输出:
代码:
If you are building your own expressions and compiling them, or using AsQueryable, then yes; the LINQ-generated methods will be a royal pain to debug.
You can save some pain by using small fragements of actual methods - at least something useful will show in the stack trace...
Another consideration is: rather than having one huge expression, if you can daisy-chain things a bit more you might have more idea (from the stack trace) where it is failing. The downside is performance - a Where(foo).Where(bar) is two delegate invokes, where-as Where(foo && bar) can be one.
One option might be to swap in a debug version of the extension methods; unfortunately it is a little inconvenient because
IQueryable<T>
andQueryable
are in the same namespace... this works, though...Output first:
Code:
如果您使用 LINQ to Objects,我不会期望看到创建动态方法。 我期望他们使用 LINQ to SQL 等。您能举一个您所看到的示例吗?
对于 LINQ,我确实没有任何好的调试技巧,但我很确定 MS 知道这是一个痛点。 我可以建议您尝试 VS2010 CTP 看看是否更好? 诚然,更多的是为了改进 VS,而不是为了解决眼前的问题。
If you're using LINQ to Objects, I wouldn't expect to see dynamic methods being created. I'd expect them with LINQ to SQL etc. Could you give an example where you're seeing this?
I don't really have any good debugging tips when it comes to LINQ, but I'm pretty sure MS know about this as a pain point. Could I suggest you try the VS2010 CTP and see if that's better? More for the sake of improving VS than for solving your immediate problem, admittedly.
看一下 用于动态方法的调试可视化工具,最初由 罗海波,并由 Roy Osherove< /a>
Take a look at the debug visualizer for dynamic methods originally developed by Haibo Luo and taken further by Roy Osherove