Thursday, November 07, 2013

Visual Studio 2012 - Cannot evaluate expression because the code of the current method is optimized

While trying to debug my C# project, I kept getting the dreaded "Cannot evaluate expression because the code of the current method is optimized" message - meaning the compiler optimized away all the details of the methods to increase execution speed. To see if your library is optimized, start the debugger and then go to "Debug/Windows/Modules" and see if your library is optimized. My problem child .dll was optimized. Right clicking on the project and selecting "Properties", I looked at the "Build" tab. I made sure the "Optimize code" checkbox was unchecked, but that is not enough.

My missing piece was on the same "Build" tab, but further down under "Advanced". I needed to set the "Output / Debug to "full", it's not enough just to be "pdb-only".

Now the project compiles with full debugging info.

2 comments:

Leo said...

In my case it was "Prefer 32" checkbox checked. After unchecking it everything worked.

Unknown said...

Thank you.!!! This one helped me!