Cannot obtain value because it has been optimized away

青春壹個敷衍的年華 提交于 2019-12-04 01:11:10

The best way I've found to convince the JIT compiler not to optimize the code is to use an INI file with the name of the assembly in the same folder the assembly is in with the contents:

[.NET Framework Debugging Control]
GenerateTrackingInfo=1
AllowOptimize=0

Note that it has to be the name of the assembly, not the name of the process EXE (unless it is the EXE assembly you want to debug). E.g. if you have an assembly MyCode.dll the INI file name would be MyCode.ini.

Here are some slides from a presentation on .Net debugging which show the difference:

With Optimization:

Without Optimization:

Go to -> Project Properties -> under Build -> set "Optimize Code" checkbox Unchecked.

Also, Under Project Properties -> Build -> Advanced -> set the "Debug Info" dropdown to "Full" in the Options

For anyone else who was stumped on this, you may be making the same simple mistake I was: my build mode was set to Release from another project I had to build for release earlier in the day. Flip it back to Debug; problem solved.

After making the changes listed by codekaizen and isanka thalagala, do not forget to clean, rebuild and publish to make the changes effective.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!