如何防止C#编译器/ CLR从优化掉在调试版本中未使用的变量? [英] How to prevent C# compiler/CLR from optimizing away unused variables in DEBUG builds?

查看:1181
本文介绍了如何防止C#编译器/ CLR从优化掉在调试版本中未使用的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试我试图计算的中间结果保存到一个变量,以便当断点条件满足我可以检查该值。但是C#编译器(或CLR)优化掉该变量为未使用。我通过该变量的类的公共领域解决了这个问题,但是我想知道如果有一个简单的解决这个问题。

While debugging I tried to save intermediate results of a calculation to a variable so that when a breakpoint condition is met I could check that value. However C# compiler (or CLR) optimized away that variable as unused. I solved the problem by making the variable a public field of the class, however I'd like to know if there is a straightforward solution to this problem.

优化代码复选框被选中。 。构建配置调试

"Optimize code" checkbox is unchecked. The build configuration is Debug.

编辑:发现这只会影响一些迭代器未使用的变量通常会最终成为在自动生成的迭代器类领域;未使用的变量未包含yield语句被保留街区范围。

Found that it only affects some unused variables in iterators that would normally end up as fields in the automatically generated iterator class; unused variables that are scoped within blocks not containing yield statements are retained.

推荐答案

懒的办法是.... 使用值的,最好的方式,不允许它举行的堆栈。例如:

The lazy option would be.... use the value, ideally in a way that doesn't allow it to be held on the stack. For example:

 var tmp = SomeMethod();
 // your other code
 Debug.WriteLine(tmp);



使用值作为参数意味着它必须被保留,但该行是自动未编译进入发布版本。

the use of the value as an argument means it must be retained, but that line is automatically not compiled into release builds.

不过!我必须强调,当地人相当,远始终保留在未优化/调试版本,所以我发现从这个问题很难想象的场景。

However! I must emphasize that locals are pretty-much always retained in an unoptimized/debug build, so I'm finding the scenario from the question hard to envisage.

这篇关于如何防止C#编译器/ CLR从优化掉在调试版本中未使用的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆