简体   繁体   English

Visual Studio - 如何在调试器中更改方法的返回值?

[英]Visual Studio - How to change the return value of a method in the debugger?

When I'm debugging, I often have to deal with methods that does not use an intermediate variable to store the return value :在调试时,我经常不得不处理不使用中间变量来存储返回值的方法:

   private int myMethod_1()
   {
      return 12;
   }

   private int myMethod_2()
   {
      return someCall( someValue );
   }

Well, in order to recreate a bug, I often have to change values on the fly.好吧,为了重新创建错误,我经常不得不动态更改值。 Here, I could want to see what happens when myMethode_1 return zero.在这里,我想看看当myMethode_1返回零时会发生什么。 Same thing for myMethod_2 . myMethod_2

Is there a way to do that without modifying the code ?有没有办法在不修改代码的情况下做到这一点? What I would like to do, it's to place a breakpoint on the return line (or on the closing bracket) and type-in a new return value.我想做的是在返回行(或右括号)上放置一个断点并输入一个新的返回值。

Note : I'm using Visual Studio 2005 and Visual Studio 2008.注意:我使用的是 Visual Studio 2005 和 Visual Studio 2008。

Thank !谢谢 !

Return values from functions are usually returned in the EAX register.函数的返回值通常在 EAX 寄存器中返回。

If you set a breakpoint just at the end of the function then there's a chance that changing EAX would change the return value.如果您在函数末尾设置断点,则更改 EAX 可能会更改返回值。 You can change and view any register in visual studio simply by writing its name in the watch window.您只需在监视窗口中写入其名称即可更改和查看 Visual Studio 中的任何寄存器。
This is likely to fail if you have optimization on or even if the function is something simple like return 12 .如果您进行了优化,或者即使函数很简单,例如return 12这也可能会失败。 it's probably also not going to work if you're returning something that doesn't fit in a 32 bit register.如果您返回的内容不适合 32 位寄存器,它也可能无法正常工作。 In the least it's worth trying.至少值得一试。

For those who are looking for a solution to this in VB.NET:对于那些在 VB.NET 中寻找解决方案的人:

It was so simple, I can't believe I did not see it : To look at the value a function will return : just place the pointer over the function's name.太简单了,我不敢相信我没有看到它:要查看函数将返回的值:只需将指针放在函数名称上即可。 The value will be shown in a tool tip.该值将显示在工具提示中。

The change the value : just click on this tool tip, change the value and hit enter.更改值:只需单击此工具提示,更改值并按 Enter。

Visual Studio is very cool ! Visual Studio 非常酷!

Note : I tested it in VB.NET on Visual Studio Team System 2008. Just tried using C#, but it does not work... :-(注意:我在 Visual Studio Team System 2008 上的 VB.NET 中对其进行了测试。刚刚尝试使用 C#,但它不起作用... :-(

Don't think so.不要这么认为。
You'd need to have a temp var in the callee or the caller so as to get a handle on the value to modify it in the IDE Debugger/QuickWatch window.您需要在被调用方或调用方中有一个临时变量,以便在 IDE Debugger/QuickWatch 窗口中获取该值的句柄以对其进行修改。 So the simplest and fastest way to do it would be to comment existing code and make a temporary change for debug-what-if.因此,最简单、最快捷的方法是注释现有代码并对调试假设进行临时更改。

either任何一个

private int myMethod_1()
{
  var x = 12;
  return x;
}

Or或者

private int myMethod_2()
{
  var y = someCall( someValue);
  return y;
}

Don't see it worth the aggravation of 'avoiding a code change' to do this.不要认为这样做值得加重“避免代码更改”。 If I FUBAR, I do a checkout and we're gold again.如果我 FUBAR,我会结帐,然后我们又是金子了。

Why do you want to change the return value in the called method?为什么要更改被调用方法中的返回值? You can easily change it on-the-fly in the Calling method.您可以在 Calling 方法中轻松地即时更改它。 As soon as your function returns, you get the value returned and there you can change it.一旦您的函数返回,您就会得到返回的值,您可以在那里更改它。

private int myMethod_1()
   {
      return 12;
   }
//call would be something like this
int x = myMethod_1();
//Here you can change the value after the execution of the above line.

Even if you are not storing return value in a variable, and you are directly passing it to some other method, even then you can change the value by stepping into that method and changing the argument value there.即使您没有将返回值存储在变量中,而是直接将其传递给某个其他方法,即使如此,您也可以通过进入该方法并在那里更改参数值来更改该值。 Am I missing something here?我在这里错过了什么吗?

What you need is Mocking.你需要的是 Mocking。 Checkout mocking frameworks like Rhinomock, Moq or Typemock.结帐模拟框架,如 Rhinomock、Moq 或 Typemock。

For the kind of scenario you are describing, I am going to assume that you may not have clean contract - driven development and may need to return in fake behaviour/state.对于您所描述的那种场景,我将假设您可能没有干净的契约驱动的开发,并且可能需要以虚假的行为/状态返回。 In this case, Typemock may be the best option for you although it is commercial在这种情况下,Typemock 可能是您的最佳选择,尽管它是商业用途

I think you can do it in Visual Studio but that feels dirty to me.我认为你可以在 Visual Studio 中做到这一点,但这对我来说很脏。

My thoughts are you should really be looking into a unit testing framework and mocking so that you can fake the output of myMethod_2 as a 0 and see what effect it has on your code.我的想法是您真的应该研究单元测试框架并进行模拟,以便您可以将 myMethod_2 的输出伪造为 0 并查看它对您的代码有什么影响。 IMO.海事组织。 This way you can leave the return 0 check in the unit test to make sure the ugly bug doesn't stick its head up again.通过这种方式,您可以在单元测试中保留 return 0 检查,以确保丑陋的错误不会再次抬起头来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Visual Studio 2010调试器中修改托管代码的方法返回值 - Modifying method return value for managed code in Visual Studio 2010 debugger Visual Studio调试器如何推断监视窗口内的Value? - How the Visual Studio debugger infer Value inside watch window? 如何在Visual Studio调试器中查看属性的整个值? - How can I view the entire value of a property in the Visual Studio debugger? 在 Microsoft Visual Studio 调试器中更改日期时间 - Change DateTime in the Microsoft Visual Studio debugger 在Visual Studio调试器中设置私有列表的值 - Set value of a private list in visual studio debugger 如何查看未与调试器一起存储的方法返回值 - How to see method return value which is not being stored with the debugger 是否可以更改第三方类的“ Visual Studio调试器变量”窗口“值”列中显示的内容? - Is it possible to change what is displayed in a Visual Studio Debugger Variables window Value column for a 3rd party class? 在Visual Studio中检查方法的返回行上的函数的返回值 - Inspecting the returned value of a function on the return line of a method in Visual Studio 在VS调试器中获取方法的返回值 - Getting a Method's Return Value in the VS Debugger 如何使用IronPython shell扩展Visual Studio调试器? - How to extend the Visual Studio Debugger with an IronPython shell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM