在 Visual Studio 2010 调试器中修改托管代码的方法返回值

发布于 2024-09-19 16:16:51 字数 305 浏览 6 评论 0原文

假设我有这个 C# 方法:

public bool GetVal()
{
    return a1 == b1 || c1 == d1 || GetE1() == GetF1(); // Illustrating complicated logic here..
}

我不想修改上面语句中变量/方法的返回值的内容,但想向调用 GetVal() 的方法返回 false。

是否可以使用 VS2010 调试器以某种方式修改返回值,就像我可以动态修改变量值一样?也许以某种方式修改调用堆栈?

Say I have this C# method:

public bool GetVal()
{
    return a1 == b1 || c1 == d1 || GetE1() == GetF1(); // Illustrating complicated logic here..
}

I don't want to modify the content of the variables / return values of methods in the statement above, but want to return false to the method that's calling GetVal().

Is it possible to use the VS2010 debugger to somehow modify the return value in the same way that I can modify variable values on the fly? Perhaps modifying the call stack somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

池予 2024-09-26 16:16:51

无法以这种方式直接修改返回值。与 C++ 相比,托管代码对查看函数调用的返回值的支持非常有限/不支持。

但是您可以做的是转到调用站点并修改分配了函数调用值的变量。

It's not possible to modify the return value directly in this manner. Managed code has very limited / no support for seeing the return value of a function call as compared to C++.

But what you can do is go to the call site and modify the variable which is assigned the value of function call.

鸵鸟症 2024-09-26 16:16:51
public bool GetVal()
{
    bool retval = a1 == b1 || c1 == d1 || GetE1() == GetF1();
// edit retval to be 'false' in the debugger now
    return retval;
}
public bool GetVal()
{
    bool retval = a1 == b1 || c1 == d1 || GetE1() == GetF1();
// edit retval to be 'false' in the debugger now
    return retval;
}
牵你手 2024-09-26 16:16:51

我经常发现自己需要同样的东西,不幸的是,唯一的方法是创建一个临时变量,您可以在函数返回之前通过“局部变量”或“监视”窗口更改该变量。

public bool GetVal()
{
    bool b = a1 == b1 || c1 == d1 || GetE1() == GetF1();
    return b;//Set breakpoint here
}

I often find myself needing the same thing, unfortunately the only way to do this is to create a temporary variable which you can change via the Locals or Watch window before the function returns.

public bool GetVal()
{
    bool b = a1 == b1 || c1 == d1 || GetE1() == GetF1();
    return b;//Set breakpoint here
}
标点 2024-09-26 16:16:51
public bool GetVal()
{
    var result = a1 == b1 || c1 == d1 || GetE1() == GetF1(); // Illustrating complicated logic here..
    return result;
}

在返回行设置断点并修改结果变量。

public bool GetVal()
{
    var result = a1 == b1 || c1 == d1 || GetE1() == GetF1(); // Illustrating complicated logic here..
    return result;
}

Set breakpoint on return line and modify result variable.

雾里花 2024-09-26 16:16:51

正如其他答案所指出的:

public bool GetVal()
{
    bool result = a1 == b1 || c1 == d1 || GetE1() == GetF1();
    return result;
}

工作完美。然而,一般来说,将大量 if 语句串在一起的代码无论如何都难以阅读和调试。

您应该将函数分解为步骤,而不是让它在一行中执行所有步骤。

完成任务的另一种方法是在代码中的 if(GetVal()) 处设置断点,然后简单地从那里修改执行路径,而不是尝试修改返回值,只需修改值已被使用的事务状态。

As the other answers noted:

public bool GetVal()
{
    bool result = a1 == b1 || c1 == d1 || GetE1() == GetF1();
    return result;
}

Works perfectly. However, in general code that strings together huge lines of if statements are much harder to read and debug anyway.

You should be breaking your function into steps rather than having it perform all of the steps in one line.

Another method to accomplish your task would be to set a breakpoint in your code at if(GetVal()) and simply modify the execution path from there, rather than trying to modify the return value, simply modify the state of affairs where the value is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文