代码之家  ›  专栏  ›  技术社区  ›  rickythefox

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

  •  1
  • rickythefox  · 技术社区  · 16 年前

    假设我有一个C方法:

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

    我不想修改上面语句中方法的变量/返回值的内容,但希望将false返回给正在调用的方法 GetVal() .

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

    5 回复  |  直到 16 年前
        1
  •  1
  •   JaredPar    16 年前

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

        2
  •  0
  •   Steve Townsend    16 年前
    public bool GetVal()
    {
        bool retval = a1 == b1 || c1 == d1 || GetE1() == GetF1();
    // edit retval to be 'false' in the debugger now
        return retval;
    }
    
        3
  •  0
  •   Brian R. Bondy    16 年前

    public bool GetVal()
    {
        bool b = a1 == b1 || c1 == d1 || GetE1() == GetF1();
        return b;//Set breakpoint here
    }
    
        4
  •  0
  •   Alex Reitbort    16 年前
    public bool GetVal()
    {
        var result = a1 == b1 || c1 == d1 || GetE1() == GetF1(); // Illustrating complicated logic here..
        return result;
    }
    

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

        5
  •  0
  •   DevinB    16 年前

    正如其他答案所指出的:

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

    效果很好。然而,通常情况下,将大量if语句串在一起的代码无论如何都很难读取和调试。

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

    if(GetVal()) 并且只需从此处修改执行路径,而不是试图修改返回值,只需修改值所在的状态 .