你可以得到抛出异常的方法叫什么名字? [英] Can you get Method name that threw Exception?

查看:420
本文介绍了你可以得到抛出异常的方法叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个C#方法,它返回到最上面的来电,的名称我的方法抛出一个异常,即使在实际的异常被抛出另一个(例如数据库驱动程序错误时,我的方法调用它)?

Is there a C# method that returns to the upper-most caller, the Name of MY Method that threw an exception, even if the actual exception was thrown by another (e.g. database driver errors when my method calls it) ?

号码 - >的MyMethod - > DbDriver(错误)

我要的MyMethod,而不是DbDriver

我宁愿不重新抛出异常一路攀升链。

I'd rather not re-throw exceptions all the way up the chain.

我目前的杂牌组装电脑,是要遍历堆栈跟踪帧,并使用解析出我的方法 MethodBase.GetCurrentMethod()。DeclaringType

My current kludge, is to iterate through the StackTrace frames, and parse out my Method using MethodBase.GetCurrentMethod().DeclaringType.

有一个内置的方法来获取这些信息,或更优雅的解决方案?

Is there a built-in method to get this information or a more elegant solution?

谢谢!

推荐答案

使用<一个href="http://msdn.microsoft.com/en-us/library/system.exception.targetsite%28VS.90%29.aspx"><$c$c>Exception.TargetSite属性来获取方法名

Use Exception.TargetSite property to get the method name

获取抛出当前异常的方法。

Gets the method that throws the current exception.

这篇关于你可以得到抛出异常的方法叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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