gpt4 book ai didi

c# - Visual Studio - 调试时将多行表达式插入监 window 口

转载 作者:太空狗 更新时间:2023-10-29 20:09:36 24 4
gpt4 key购买 nike

在 Visual Studio 中进行调试时,如何将多行表达式插入到监 window 口中,这样每一行就不会被分成单独的无效监视表达式。这真的很令人沮丧,因为我有很多跨越多行的表达式需要观看。请注意,Pin to Source 和 Immediate Window 都不适用于跟踪源代码中多个位置的多个值。

例如

PyFunc1(Py.kw("var1", var1),
Py.kw("var2", var2))

被破坏为:

PyFunc1(Py.kw("var1", var1),

Py.kw("var2", var2))

最佳答案

复制

我不认为这是“按设计”,它只是“开箱即用”不可用。

我同意,使用行终止符而不是新行将多行调用添加到监 window 口会更好:

enter image description here


研究

我发现这个类似的问题有一些“解决方法”可供选择: Multi-Line Watch Window in Visual Studio 2010?

我还在 MSDN Forums by a MSFT Engineer 中找到了这条评论:

I’m afraid that it is not supported, we often edit them one by one. Maybe you could submit this feature request: http://visualstudio.uservoice.com/forums/121579-visual-studio


推出您自己的 Visual Studio 插件

所以我自己尝试了一下,这绝不是生产代码,但它向您展示了如何去做:

(点击图片放大)

enter image description here

namespace AddinMultiLineWatch
{
public class Connect : IDTExtensibility2, IDTCommandTarget
{
//ADD THESE MEMBER VARIABLES
//private DebuggerEvents _debuggerEvents = null;
//private _dispDebuggerEvents_OnEnterBreakModeEventHandler DebuggerEvents_OnEnterBreakMode;
private Window _watchWindow = null;
private CommandEvents _objCommandEvents;
private bool _isRecursive = false;
public Connect()
{
}

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;

//SET THE MEMBER VARIABLES
//_debuggerEvents = _applicationObject.Events.DebuggerEvents;
//_debuggerEvents.OnEnterBreakMode += new _dispDebuggerEvents_OnEnterBreakModeEventHandler(BreakHandler);
//var watchWindow = _applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindWatch);
_objCommandEvents = _applicationObject.Events.CommandEvents;
_objCommandEvents.BeforeExecute += new _dispCommandEvents_BeforeExecuteEventHandler(BeforeExecute);

if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
string toolsMenuName = "Tools";

Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];
ar:
CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

try
{
Command command = commands.AddNamedCommand2(_addInInstance, "AddinMultiLineWatch", "AddinMultiLineWatch", "Executes the command for AddinMultiLineWatch", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

if((command != null) && (toolsPopup != null))
{
command.AddControl(toolsPopup.CommandBar, 1);
}
}
catch(System.ArgumentException)
{
}
}
}

//ADD THIS METHOD TO INTERCEPT THE DEBUG.ADDWATCH COMMAND
public void BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
EnvDTE.Command objCommand = default(EnvDTE.Command);
try
{
objCommand = _applicationObject.Commands.Item(Guid, ID);
}
catch (Exception ex)
{
}

if ((objCommand != null))
{
if (objCommand.Name == "Debug.AddWatch")
{
//if (_isRecursive) return;
//_isRecursive = true;
TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
//TODO make selection goto next semi-colon/Line Terminator...
var selText = selection.Text;

if (string.IsNullOrEmpty(selText)) return;
//Only intercept multi-line Add Watch commands
if (selText.Contains(Environment.NewLine))
{
//THE BLACK MAGIC: make it fit in one line! lol
selText = selText.Replace(Environment.NewLine, string.Empty);
//THIS CALL IS RECURSIVE, I'LL LEAVE IT TO THE READER AS AN EXERCISE TO SOLVE..
_applicationObject.ExecuteCommand("Debug.AddWatch", selText);
}
}
}
}
  1. 创建一个新项目 > 其他项目类型 > 可扩展性 > Visual Studio 加载项 > 将其命名为 AddinMultiLineWatch

  2. 完成向导

  3. 将上面的代码添加到 Connect.cs 类 - 请参阅我的//UPPERCASE 注释以及要添加的内容。

  4. TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection; 行上打一个断点;

  5. 按 F5 将启动一个新的 VS 实例 > 选择“新建项目”>“控制台应用程序”> 将其命名为 TestMultilineAddWatch

  6. 在Console App的program.cs中,指定2行以上的代码调用,并打上断点,如截图所示,eg:

    Add(1,            //Breakpoint here and select both lines
    2);
    }

    static int Add(int i, int j)
    {
    return i + j;
    }
  7. F5 在 TestMultilineAddWatch 解决方案中,当代码控件在断点处停止时 > 选择/突出显示两行 Add(1,\r\n 2) > 右键单击​​ > 添加观看

  8. 在 VS IDE 调试上下文菜单中单击 Add Watch 会导致 VS AddinMultiLineWatch 解决方案拦截调用并激活,在断点处暂停....您将在其中看到 将多行代码替换为单行代码的黑魔法发送到监 window 口。

Visual Studio EXEC 命令调用自身使该方法递归,如果您调试它,手动退出递归,您将看到我的屏幕截图中的结果。

调试愉快!

关于c# - Visual Studio - 调试时将多行表达式插入监 window 口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38337525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com