简体   繁体   English

仅当未达到默认情况时,才可能中断switch语句吗?

[英]Is it possible to break on a switch statement only when it doesn't hit the default case?

THere's a switch statement with 8 cases inside, and I want to see when it hits. 这是一个内部有8个案例的switch语句,我想看看它何时触发。

Unfortunately before hitting, it hits default around 10 times. 不幸的是,在命中之前,它的默认命中率约为10倍。

Now I either break at switch, press F10, and continue when it hits default, or I set breakpoints on each case but default. 现在,我要么在开关处断开,按F10键,然后在达到默认值时继续操作,或者在每种情况下(但默认情况下)设置断点。

Any chance I can change this? 我有机会改变这一点吗?

It's not possible to change the switch statement or the way the methods around it work (unfortunately). 不幸的是,无法更改switch语句或其周围方法的工作方式。

Edit: I'm looking for something like a conditional breakpoint, but the condition being switchOnThis == (default case) 编辑:我正在寻找类似条件断点的东西,但条件是switchOnThis == (default case)

You can set a conditional breakpoint like the following. 您可以像下面那样设置条件断点。 First suppose you have the following switch\\case : 首先,假设您有以下switch\\case

int test = 0;
// value of the test will be set here
switch (test)
{
    case 1:
        // case 1
        break;
    case 4:
        // case 4
        break;
    case 5:
        // case 5
        break;
    // up to 8 cases
    default:
        break;
}

Then set a breakpoint in the editor, and right-click on it. 然后在编辑器中设置一个断点,然后右键单击它。 You can see the Conditions... on the menu. 您可以在菜单上看到Conditions... click on it, and write the conditions: 单击它,并写下条件:

在此处输入图片说明

You can set the conditions like the following: 您可以设置以下条件:

在此处输入图片说明

The conditional berakpoint breaks when one of the cases will be happened. 当其中一种情况发生时,有条件的berakpoint中断。

I came up with something. 我想出了点什么。

You could take advantage of the side effects of breakpoint conditions this way: 您可以通过以下方式利用断点条件的副作用:

  1. Place a breakpoint before (or at) the switch statement. switch语句之前(或之前)放置一个断点。 The condition on this breakpoint should "raise a flag" (eg create a file) and evaluate to anything other than true , so that the breakpoint is not hit. 此断点上的条件应“升起一个标志”(例如,创建文件),并求值为true以外的任何true ,以便不击中该断点。
  2. Place a breakpoint in the default case. default情况下放置一个断点。 The condition on this breakpoint should "lower the flag" (eg delete the same file) and should also not evaluate to true . 此断点上的条件应“降低标志”(例如,删除同一文件),并且也不应评估为true
  3. Place a breakpoint at the instruction following the end of the switch block. switch块末尾的指令处放置一个断点。 Condition on this breakpoint should simply check the flag (eg check that file exists) and return true if the flag is still raised (ie a case other than default was hit). 此断点上的条件应仅检查标志(例如,检查文件是否存在),如果标志仍被引发(即,遇到非默认情况),则返回true This is where execution will break when one of the case labels other than default is hit . 这是在命中除默认值以外的任一case标签时执行将中断的地方

I tried this with the following code: 我用以下代码尝试了此操作:

static void Main(string[] args)
{
    int num = 0;
    bool hit = false;
    do
    {
        Console.WriteLine("Insert an integer value (1-8 hits, 100 to quit):");
        num = Convert.ToInt32(Console.ReadLine());

        switch (num) // breakpoint 1 here
        {
            case 1: hit = true; break;
            case 2: hit = true; break;
            case 3: hit = true; break;
            case 4: hit = true; break;
            case 5: hit = true; break;
            case 6: hit = true; break;
            case 7: hit = true; break;
            case 8: hit = true; break;
            default: hit = false; break; // breakpoint 2 here
        }

        Console.WriteLine("hit = " + hit.ToString()); // breakpoint 3 here
    } while (num != 100);

}

I placed conditional breakpoint 1 at switch (num) with the following condition (it evaluates to nothing, thus it never breaks): 我将条件断点1设置为具有以下条件的switch (num) (其结果为空,因此它永不中断):

System.IO.File.Create(@"somefile.txt").Close()

Breakpoint 2 at the default case with the following condition (also evaluates to nothing): default情况下的断点2具有以下条件(也没有结果):

System.IO.File.Delete(@"somefile.txt")

Finally, breakpoint 3 at Console.WriteLine("hit =... , with condition: 最后,在Console.WriteLine("hit =...处的断点3,条件为:

System.IO.File.Exists(@"somefile.txt")

It works as expexted, ie it breaks after the switch block only if the default case was not hit. 它以扩展方式工作,即,仅当未击中default情况时,它才会在switch块之后switch

In order for this to work you need to configure the debugging environment to use both native and managed compatibility . 为了使其正常工作, 您需要配置调试环境以使用本机兼容性和托管兼容性 Go to: 去:

Tools -> Options -> Debugging -> General

and check Use Managed Compatibility Mode and Use Native Compatibility Mode . 并选中“ Use Managed Compatibility Mode和“ Use Native Compatibility Mode

The create/delete file idea is the only one I could come up with that has side effects and doesn't require any change to your code. 创建/删除文件的想法是我能想到的唯一具有副作用并且不需要对代码进行任何更改的想法。

Add another switch with the same cases before your existing switch : 添加另一个switch现有的与以前相同的情况下, switch

switch (condition)
{
  case 1:
  case 2:
  ...
  case 8:
    break;  // <-- set the breakpoint here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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