4

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?

4

1 回答 1

0

此信息显然是从调试信息中提取的。StackFrames因此,它并不总是可用的,我想这就是为什么在这种情况下对象不实现它是有意义的。

无论如何,要获得包含文件和行号信息(以及 IL 偏移量等)的堆栈跟踪,您必须在已调试应用程序的上下文中动态执行代码。您可以使用GetExpression.

总之:

var tmp = dte.Debugger.GetExpression(
    "new System.Diagnostics.StackTrace(true).ToString();", true);

这将返回一个带有堆栈跟踪的字符串,包括文件和行号......但是,您必须解析这个返回的字符串才能实际使用它,我认为这比更常见的方法慢得多

于 2013-01-10T05:57:07.700 回答