5

I want to reset password inside local function. Suddenly, i tried to run this local function from quick watch, but it didn't work.

enter image description here

Please help me out, if i am doing anything wrong. Thanks in advance.

jitendra joshi
  • 677
  • 5
  • 18
  • 1
    QuickWatch is for watching the values of variables, not executing code... – Herohtar Oct 12 '19 at 05:11
  • 1
    Try use immediate window... – Johnny Oct 12 '19 at 05:42
  • 3
    But i am able to run other functions in QuickWatch.... It is showing expression has been evaluated and has no value. – jitendra joshi Oct 12 '19 at 07:58
  • I m not worried about the output, i just want to execute the function.. Immediate window is also showing the same error message.. error CS0103: The name 'ResetPasswordLocal' does not exist in the current context – jitendra joshi Oct 12 '19 at 08:04
  • 1
    Be sure to understand "current context". Since this is an instance method, the debugger can only recognize it if you currently have a breakpoint active in one of the methods of the class. Once you got that correct, you will however discover that it doesn't like dealing with await all that much either. Or Task for that matter. You can't expect to have threads running correctly when there's an active breakpoint. – Hans Passant Oct 13 '19 at 21:28
  • It unfortunately doesn't work indeed (same in VS17.0.5), and [the corresponding ticket](https://developercommunity.visualstudio.com/t/Cannot-debug-local-function-in-Watch-Im/1106423) has been closed due to little activity :( – JBSnorro Feb 12 '22 at 13:13

2 Answers2

4

The compiler changes the name of the local function, preventing you from calling it using its original name from the debugger. See this question for examples. What you can do is temporarily modify the code to save a reference to the local function in a delegate variable. After recompiling, you can invoke the function through the delegate variable from Quick Watch or the Immediate Window. In your case, add this code to the beginning of the method:

Func<string,Task> f = ResetPasswordLocal;

Now you can invoke f in Quick Watch.

Tom Minka
  • 794
  • 6
  • 10
1

I'll have to say that I haven't tried it and will not bother to do so because there's a lot more to local functions than you think and I would put it very low in terms of priority for the debugger.

Try putting your code in sharplab.io and see what it takes to make that local function.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59