简体   繁体   中英

How can I get Visual Studio 2017 to interactively debug this line?

I have the following code set up as a dummy example to illustrate what I found in my production code.

static void Main(string[] args)
{
    bool GreaterThan(int x, int y)
    {
        return x > y;
    }

    bool OperateOnTwoNumbers(int x, int y, Func<int, int, bool> func)
    {
        return func(x, y);
    }

    var twoGreaterThanOne = OperateOnTwoNumbers(2, 1, GreaterThan);

}

When I set a watch, or interatively debug with Shift+F9 , on OperateOnTwoNumbers(2, 1, GreaterThan) , I get the following error.

OperateOnTwoNumbers(2, 1, GreaterThan) error CS0103: The name 'OperateOnTwoNumbers' does not exist in the current context

But the code itself runs fine, and after I step over the line, I can see the value assigned to the variable.

This has been replicated on more than one computer, with the production code and with this dummy example, so I don't think it's an environment issue.

Here is a recording of the issue, it can be viewed in full quality by clicking on the recording and viewing it at its direct address.

记录问题

Any ideas?

I suspect that watch expression evaluator has not yet been updated to handle local functions (or, if it works in some versions of VS and doesn't in others - has not been updated in your specific version).

Local functions (like GreaterThan and OperateOnTwoNumbers ) are just syntax sugar and really compiled into static functions with cryptic names:

[CompilerGenerated]
internal static bool <Main>g__OperateOnTwoNumbers|0_1(int x, int y, Func<int, int, bool> func)
{
  return func(x, y);
}

And call to them is then:

Program.<Main>g__OperateOnTwoNumbers|0_1(...);

So watch expression evaluator should realize that, but it doesn't and tries to call function with OperateOnTwoNumbers name (as you can see from compiler error message "The name 'OperateOnTwoNumbers' does not exist in the current context"), and there is really no such function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM