在C#(或一般.NET)中,您可以屏蔽通过属性引发异常的调用堆栈级别吗? [英] In C# (or .NET in general) can you mask the call stack level where an exception was thrown via attributes?

查看:128
本文介绍了在C#(或一般.NET)中,您可以屏蔽通过属性引发异常的调用堆栈级别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能会有些混乱,所以我会解释。假设您有这个呼叫链...

  public DoWork(index)>>私人DoWorkHelper(index)>>私人CheckIndex(index)

现在,如果您调用 DoWork ,它将遍历调用向下到 CheckIndex ,将每个更深的调用添加到调用堆栈中。



现在,如果有人调用 DoWork 并为 index 设置了一个错误的值,它将异常抛出到 CheckIndex ,当前就是调试器中断的地方。然后,您必须返回呼叫堆栈,以查看真正的犯罪者是某人将错误数据传递给 DoWork



现在回到VB6天,您可以简单地装饰 DoWorkHelper CheckIndex 的属性,说如果有在我内部抛出异常,请不要高亮我,而是我的呼叫者,因为它们是使我无法通过的原因!'因此,在这种情况下,代码将在 DoWork ,突出显示了对 DoWorkHeper 的调用。



还有一个禁用此设置的功能,以便出于更深入的调试目的,仍可以将其扔到 CheckIndex ,实际发生的级别,但有一半时间,在那里分解不会告诉您任何信息,因为您不知道如何到达那里而没有返回调用堆栈。



想想这是一种修饰代码的方法,以便说出遇到异常时自动将调用堆栈遍历到错误值实际上告诉您有用的地方。



请注意,这类似于打破所有例外,不同之处在于您通过装饰来处理。另外,您不是要设置打破特定类型的异常(例如,所有空引用异常等),而是打破特定的方法! (或更准确地说,是一种称为修饰方法的方法。)



那么C#或.NET通常具有这种功能吗?






更新



虽然我将Dark Falcon的答案归功于他的指导,但我还是将其归功于他有关所有属性的含义以及上下文的更详细说明。

解决方案

请参阅只是我的代码选项。您需要用 DebuggerHiddenAttribute DebuggerNonUserCodeAttribute 装饰 DoWorkHelper 。 / p>

The title may be a little confusing so I'll explain. Say you had this call chain...

public DoWork(index) >> private DoWorkHelper(index) >> private CheckIndex(index)

Now if you call DoWork, it traverses the calls down to CheckIndex, adding each deeper call to the call stack.

Now if someone calls DoWork with a bad value for index, it throws the exception all the way down at CheckIndex, and currently, that's where the debugger breaks. You then have to walk back up the call stack to see the real offender was that someone passed bad data to DoWork.

Now back in the VB6 days, you could simply decorate DoWorkHelper and CheckIndex with an attribute to say 'If any exception is thrown inside me, don't highlight me, but rather my caller because they were the ones that passed me bad crap!' So in this case, the code would break inside DoWork with the call to DoWorkHeper highlighted.

There was also a setting to disable this so for deeper debugging purposes it could still be thrown at CheckIndex, the level where it actually occurred, but half the time, breaking down there tells you nothing because you don't know how you got there without walking back up the call stack.

Think of it a way to decorate your code to say when you hit an exception, auto-traverse the call stack to the point where the bad value actually tells you something useful.

Note this is similar to 'Break On All Exceptions' except you're handling this via decoration. Plus, you're not setting to break on a specific type of exception (e.g. all null reference exceptions, etc.) but rather a specific method! (or more accurately, the one that called the decorated method.)

So does C# or .NET in general have this?


Update

While I credit Dark Falcon for the answer since he directed me there, I've added a more detailed explanation about what all the attributes mean, and in what context. Check it out below.

解决方案

Please see the Just My Code option. You need to decorate DoWorkHelper with DebuggerHiddenAttribute or DebuggerNonUserCodeAttribute.

这篇关于在C#(或一般.NET)中,您可以屏蔽通过属性引发异常的调用堆栈级别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆