如何获得断裂/异常在Visual Studio插件当前的行号? [英] How to get the current line number at break/exception in Visual Studio add-in?
问题描述
我与Visual Studio乱搞附加API想看看是否有我想做的事情是可能的。有一件事我现在正在做的是一样的东西:
I'm messing around with the Visual Studio add-in API trying to see if something I want to do is possible. One thing I am doing right now is something like:
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
_applicationObject.Events.DebuggerEvents.OnExceptionThrown += DebuggerEvents_OnExceptionThrown;
handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin1.Connect.MyAddin1")
{
handled = true;
return;
}
}
}
void DebuggerEvents_OnExceptionThrown(string ExceptionType, string Name, int Code, string Description, ref dbgExceptionAction ExceptionAction)
{
//how to get line number here?
}
在理想情况下,我希望能够得到当前函数和行正在调试每当一个异常被抛出的程序数量。这可能吗?
Ideally, I'd like to be able to get the current function and line number whenever an exception is thrown by a program being debugged. Is this possible?
推荐答案
这信息显然是从调试信息解除。因此,它并不总是可用的,我想这就是为什么它使得在 StackFrames
对象在这种情况下没有实现它的意义。
This information is apparently lifted from the debug information. As such, it isn't always available, I guess this is why it makes sense for the StackFrames
object to not implement it in this case.
总之,要获得文件和行号信息的堆栈跟踪(和IL偏移等),您必须在调试应用程序的环境中动态执行代码。为此,您可以使用 GetExpression
。
Anyway, to get a stack trace with file and line number information(and IL offset, etc) you have to dynamically execute code within the debugged application's context. You can do this using GetExpression
.
在总结:
var tmp = dte.Debugger.GetExpression(
"new System.Diagnostics.StackTrace(true).ToString();", true);
这将返回一个字符串的堆栈跟踪,包括文件和行号......不过,你必须分析这个返回的字符串来实际利用它,我以为这是远远低于更常用的方法
This will return a string with the stack trace, including file and line numbers... However, you must parse this returned string to actually make use of it, and I assume that this is much slower than the more common method
这篇关于如何获得断裂/异常在Visual Studio插件当前的行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!