【问题标题】:DebuggerStepThrough Attribute - How to also skip child MethodsDebuggerStepThrough 属性 - 如何也跳过子方法
【发布时间】:2020-07-18 15:57:29
【问题描述】:

在 Visual Studio 调试器中工作时,我一直在使用 System.Diagnostics.DebuggerStepThrough 属性来跳过代码。
但是,有时我希望它也跳过从我应用了 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() 内停止。

我希望有一种方法可以让我不必将此属性应用于从 Parent 方法调用的所有方法。
也许我只是缺少一些简单的东西。

【问题讨论】:

    标签: c# .net system.diagnostics


    【解决方案1】:

    DebuggerStepperBoundaryAttribute 添加到您希望在单步执行时跳过的方法。

    在示例代码中,当执行在 Method1() 调用处停止并且您逐步执行代码时,而不是在 Method2 内结束,代码Console.WriteLine("Suddenly here") 将继续执行(当然Method1Method2 中的代码都在运行):

    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");
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多