简体   繁体   English

从自定义类库引发异常

[英]Throwing an exception from custom class library

I have created a library in C# Visual Studio 2012. 我已经在C#Visual Studio 2012中创建了一个库。

In this library I have areas that intentionally throw new exceptions when conditions aren't met. 在此库中,我有一些区域在不满足条件时有意引发新的异常。 I have built the library and referenced the .dll in another project. 我已经建立了该库,并在另一个项目中引用了.dll。

When the library throws an exception the debugger grabs the referenced library, opens the .cs file and displays the "Throw Exception" code from the library, instead of the code that caused the exception in my project. 当库引发异常时,调试器将获取引用的库,打开.cs文件并显示库中的“ Throw Exception”代码,而不是导致我的项目中发生异常的代码。

How can I lock the library so the debugger doesn't show the library code that threw the exception, but instead shows the project code that caused the library to throw the exception. 我如何锁定库,以便调试器不显示引发异常的库代码,而是显示导致库引发异常的项目代码。

I have tried googling, but I am afraid my lack of nomenclature knowledge is leading me to the wrong areas. 我曾尝试使用Google谷歌搜索,但恐怕我对术语的了解不足导致我进入了错误的领域。

You can mark your class and/or methods with the DebuggerStepThrough attribute. 您可以使用DebuggerStepThrough属性标记您的类和/或方法。 From the docs: 从文档:

This attribute avoids having to step into compiler-provided code and only steps into developer-provided code. 此属性避免了必须进入编译器提供的代码,而仅需进入开发人员提供的代码。 For example, if you are stepping through code by using the F11 (Step Into) key, the attribute will cause the step to behave like an F10 (Step Over) key for compiler-provided code. 例如,如果您使用F11(单步执行)键单步执行代码,则该属性将导致该步骤的行为类似于编译器提供的代码的F10(单步执行)键。 The method won't be stepped into, but it will be executed. 该方法不会被介入,但是会被执行。

For example: 例如:

[DebuggerStepThrough]
public class MyLibrary
{
    public void DoSomething()
    {
        //Unimportant code here
    }
}

Alternatively, just reference your compiled library and make sure there are no PDB files for Visual Studio to read. 另外,只需引用您的编译库,并确保没有供Visual Studio读取的PDB文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM