在调用堆栈上创建条件断点

时间:2020-01-16 09:55:04

标签: c# debugging stack breakpoints callstack

我有一个通用函数,可用于许多不同类的许多不同对象中。我想在该函数上设置一个断点,该断点仅在存在调用堆栈的类时才触发。

例如,我有这些[元]调用堆栈

myFunc()
Intermediate.class.intermediateFunction()
Interesting.class.interestingFunction()

myFunc()
Intermediate.class.intermediateFunction()
Boring.class.boringFunction()

我想在myFunc()中设置一个断点,该断点仅在从interestingFunction()函数间接调用该断点时才激活。

1 个答案:

答案 0 :(得分:1)

您可以使用System.Diagnostics命名空间以编程方式查询堆栈跟踪。 Yoy可能会执行以下操作:

System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
var f = st.GetFrames();
var names = f.Select(f => f.GetMethod().Name).ToList();
if (names.Contains("DoSomething4"))
{
    var a = 0; // Set breakpoint in this line or use Debugger.Launch()
}

您可以使用#if DEBUG和#endif以便此代码不会发布

此外,您可以使用此类为断点创建条件

相关问题