我有一个通用的函数,可用于许多不同类的许多不同对象中。我想在该函数上设置一个断点,该断点仅在调用栈存在一个类时才触发。
例如,我有这些[元]调用堆栈
myFunc()
Intermediate.class.intermediateFunction()
Interesting.class.interestingFunction()
myFunc()
Intermediate.class.intermediateFunction()
Boring.class.boringFunction()
我想在myFunc()
中设置一个断点,仅当从interestingFunction()
函数间接调用它时才激活。
您可以使用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,这样该代码就不会发布
此外,您也可以使用此类为断点创建条件