代码之家  ›  专栏  ›  技术社区  ›  Code Novice

debuggersteptthrough属性-如何同时跳过子方法

  •  2
  • Code Novice  · 技术社区  · 6 年前

    我一直在使用 System.Diagnostics.DebuggerStepThrough 在Visual Studio调试器中工作时跳过代码的属性。
    但是,有时我希望它也跳过从应用了 DebuggerStepThrough 属性。

    有办法吗?
    我不希望这会影响我应用此属性的所有方法,但是有时我不希望任何代码被调用/用于为我应用此属性的方法中调用的所有方法打开调试器。

    static void main(string[] args)
    {
        Method1();
    }
    
    [DebuggerStepThrough()]
    private static void Method1()
    {
        Method2(); 'The Debugger is stopping in Method2 when I am manually stepping through the code
    }
    
    private static void Method2()
    {
        '... Code I don't care about however debugger is stopping here.
    }
    

    所以上面的代码示例就是我遇到的一个例子。
    有没有方法让我告诉Visual Studio也跳过从内部调用的方法 Method1() ?
    当前,当我在Visual Studio中手动单步执行代码时,我发现必须添加 [DebuggerStepThrough()] 属性来调用所有方法,即使它们是从应用了属性的方法中调用的。在本例中,调试器将在内部停止 Method2() .

    我希望有一种方法可以使我不必将此属性应用于从 起源 方法。
    也许这件事很容易让我错过。

    1 回复  |  直到 6 年前
        1
  •  6
  •   Jimi    6 年前

    添加一个 DebuggerStepperBoundaryAttribute 到单步执行时要跳过的方法。

    在示例代码中,当执行在 Method1() 调用并逐步执行代码,而不是结束在内部 Method2 ,代码执行将继续 Console.WriteLine("Suddenly here") (当然,这两个代码 Method1 方法2 运行:

    static void main(string[] args)
    {
        Method1();
        Console.WriteLine("Suddenly here");
    }
    
    [DebuggerStepThrough, DebuggerStepperBoundary]
    private static void Method1()
    {
        Method2();
    }
    
    private static void Method2()
    {
        //Skipped
        Console.WriteLine("Skipped but still printing");
    }