c#Visual Studio似乎遵循错误的代码路径 [英] c# Visual Studio appears to follow an incorrect code path
问题描述
下面是从一个相当大的应用程序中摘录的一小段代码,以简化我的问题.我之前曾提交过这个问题,但是措辞不佳导致我在设法编辑问题之前将其关闭.
这是当前的代码段,我在其中添加了一些日志记录行.
int i = 0;
Console.WriteLine("Before brackets");
if (i < 0)
{
Console.WriteLine("Inside brackets");
return MyArray[i];
}
使用VS调试时,我看到:
i set to 0
if evaluates as false (when I hover over it in VS)
In Output: Before brackets
然后调试器将括号内的内容放入 内,并执行了return MyArray[i]
,但是在我逐步进入return MyArray[i]
行时,在输出中没有看到Inside brackets
. /p>
对我来说,这种行为显然是错误的,我想知道是否有人遇到过这种情况.
我正在使用VS10和.Net4.0的Windows XP 64位计算机上.
月亮
ADDITIONAL1
Henk要求我提供一个控制台应用程序",下面已完成.但是,正如我怀疑的那样,它不显示此问题.我相信还有其他问题(线程?)在我的真实应用程序中引起了我的问题-很明显,我可以发布该问题.我知道我没有给您一个明确定义的问题-如果可以的话,我会的.这就是为什么我要问这个问题-如果它被某人碰到类似东西而打断了电线.为了井井有条,这是没有出现问题的控制台版本.我觉得在这里穿上它会加剧混乱...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication2
{
class Program
{
static string[] Mods = new string[] { "Cat", "Dog", "Geek" };
static void Main(string[] args)
{
string t = "Geek";
Console.WriteLine("Answer: " + FindD(t));
}
public static string FindD(string ModelFullName)
{
int ix = Array.IndexOf<string>(Mods, ModelFullName);
Console.WriteLine("ix: " + ix + " = " + (ix < 0).ToString());
if (ix < 0)
{
Console.WriteLine("2ix: " + ix + " = " + (ix < 0).ToString());
Error.Process(ErrorLevel.Critical, "ModelName not found: " + ModelFullName);
}
try
{
return Mods[ix];
}
catch (Exception)
{
Error.Process(ErrorLevel.Critical, "Could not point to Mod for: " + ModelFullName);
}
return null;
}
enum ErrorLevel { Note, Critical };
class Error
{
public static void Process(ErrorLevel EL, string message)
{
if (EL == ErrorLevel.Critical)
{
throw new Exception("Critical error: " + GetStackTrace() + message);
}
}
public static string GetStackTrace()
{
StackTrace stackTrace = new StackTrace(); // get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames)
string st = "";
// write call stack method names
for (int i = 6; i > 1; i--)
{
StackFrame stackFrame = stackFrames[i];
st = st + stackFrame.GetMethod().Name + "/";
}
return st;
}
}
}
}
附加2
似乎我的问题不是只与VS调试器一起执行错误路线的执行路径之一.即看起来好像我在括号内,因为调试器在最后一条语句上踩,但我实际上得到的结果似乎正确.
从Google网络搜索中找到提示后,我设法解决了该问题.看来我在"Optmise Code"
模式下运行调试器.这是我早期检查过的内容,但是我没有发现什么,尽管我在调试模式下取消选择了此选项-我在标准工具栏中选择了发布".当我切换到"Debug
"时,问题就消失了.
Below is a small snippet taken from a fairly large app to simplify my question. I submitted this question before, but my poor wording caused it to be closed before I managed to edit my question.
This is the current snippet to which I have added some logging lines.
int i = 0;
Console.WriteLine("Before brackets");
if (i < 0)
{
Console.WriteLine("Inside brackets");
return MyArray[i];
}
When I debug with VS I see:
i set to 0
if evaluates as false (when I hover over it in VS)
In Output: Before brackets
Then the debugger steps inside the brackets and the return MyArray[i]
is executed however I do not see Inside brackets
in the output by the time I have stepped through to the return MyArray[i]
line.
This behaviour is obviously (to me) wrong and I wondered if anyone else had encountered something like this.
I am on a Windows XP, 64bit machine , with VS10 and .Net4.0.
Moon
ADDITIONAL1
I have been asked by Henk to provide a "console App" which I have done below. HOWEVER, as I would suspect, it does NOT SHOW the issue. I believe there is something else (threads?) causing me an in issue in my real app - and I obviously can post that. I realise I am not giving you a clearly defined problem - if I could, I would. That is why I am asking this question - incase it strikes a cord with somebody having something similar. For good order, here is console version which DOES NOT exhibit the problem. I have a feeling putting it on here will add to the confusion...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication2
{
class Program
{
static string[] Mods = new string[] { "Cat", "Dog", "Geek" };
static void Main(string[] args)
{
string t = "Geek";
Console.WriteLine("Answer: " + FindD(t));
}
public static string FindD(string ModelFullName)
{
int ix = Array.IndexOf<string>(Mods, ModelFullName);
Console.WriteLine("ix: " + ix + " = " + (ix < 0).ToString());
if (ix < 0)
{
Console.WriteLine("2ix: " + ix + " = " + (ix < 0).ToString());
Error.Process(ErrorLevel.Critical, "ModelName not found: " + ModelFullName);
}
try
{
return Mods[ix];
}
catch (Exception)
{
Error.Process(ErrorLevel.Critical, "Could not point to Mod for: " + ModelFullName);
}
return null;
}
enum ErrorLevel { Note, Critical };
class Error
{
public static void Process(ErrorLevel EL, string message)
{
if (EL == ErrorLevel.Critical)
{
throw new Exception("Critical error: " + GetStackTrace() + message);
}
}
public static string GetStackTrace()
{
StackTrace stackTrace = new StackTrace(); // get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames)
string st = "";
// write call stack method names
for (int i = 6; i > 1; i--)
{
StackFrame stackFrame = stackFrames[i];
st = st + stackFrame.GetMethod().Name + "/";
}
return st;
}
}
}
}
ADDITIONAL 2
It seems that my issue is NOT one of the execution path going a wrong route ONLY VS debugger. i.e. It appears as if I am going inside the brackets, because debugger steps on the last statement, but the results I actually get appear correct.
I have managed to resolve the issue after a tip I found from a google web search. It seems I was running the debugger in "Optmise Code"
mode. This is something I checked early on, BUT what I didn't spot wad that although I had this option deselected for Debug Mode - I had "Release" selected in the Standard Toolbar. When I switched to "Debug
" the issue went away.
这篇关于c#Visual Studio似乎遵循错误的代码路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!