问题描述
是否可以在Visual Studio(C#调试器)中抑制特定代码行的第一次机会抑制?
Is it possible to suppress first chance supressions in Visual Studio (C# debugger) for specific lines of code?
我想在调试器中使用第一次机会异常,但是在我得到有趣的代码之前,我需要经历每个调试会话约50个第一次机会异常。
I want to use first chance exceptions in the debugger, but there are about 50 first chance exceptions I need to go through every debug session before I get to the interesting code.
目前,我关闭第一次机会异常,然后手动把它们打开,但这是一个麻烦和时间沉淀。
Currently, I turn off first chance exceptions and then manually turn them on, but that's a hassle and a time sink.
推荐答案
DebuggerNonUserCodeAttribute Class
从.NET 2.0开始,如果您使用属性,调试器将跳过其中的第一次机会异常。
DebuggerNonUserCodeAttribute Class
As of .NET 2.0, if you mark an method with the [DebuggerNonUserCode] attribute, the debugger will skip first chance exceptions in it.
从MSDN链接引用(重点是我的):
Quote from MSDN link (emphasis added is mine):
与此属性相关联的调试除外,没有运行时行为。
There is no runtime behaviour apart from debugging, associated with this attribute.
但是,如果您只有一种方法用于包含在Visual Studio的第一次机会异常处理机制中的某些行,以及要排除的其他行,则可能没有解决方案这个级别的粒度。您可以随时将大型方法重构为多个方法,并在选定的方法上使用该属性。
However if you have just one method with certain lines intended for inclusion in Visual Studio's first chance exception handling mechanism, and other lines to be excluded, there's likely not a solution at this level of granularity. You can always refactor a large method into multiple methods and use the attribute on select ones.
其他信息...
的使用示例
using System.Diagnostics;
using XL = Microsoft.Office.Interop.Excel;
public static class WorkbookExtensions
{
[DebuggerNonUserCode]
public static bool TryGetWorksheet(this XL.Workbook wb, string worksheetName, out XL.Worksheet retrievedWorksheet)
{
bool exists = false;
retrievedWorksheet = null;
try
{
retrievedWorksheet = GetWorksheet(wb, worksheetName);
exists = retrievedWorksheet != null;
}
catch(COMException)
{
exists = false;
}
return exists;
}
[DebuggerNonUserCode]
public static XL.Worksheet GetWorksheet(this XL.Workbook wb, string worksheetName)
{
return wb.Worksheets.get_Item(worksheetName) as XL.Worksheet;
}
}
显示可能有用的相关VS项目选项。
The article shows related VS project options that might be useful.
这篇关于抑制第一次机会例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!