.net:调试器突出显示未实际执行的代码行

时间:2010-03-10 23:22:52

标签: c#

将以下内容复制并粘贴到VS中的新控制台应用程序中。添加对System.Web和System.Web.Services的引用(我知道控制台应用程序不需要这些程序集,我只是向您展示在我的Web应用程序中不起作用的代码片段。)

虽然if语句中的两个条件都是false,但结果却是真的。谁知道原因? (Visual Studio 2008 9.0.30729.1).NET 3.5 SP1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using System.Web;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = "qweqweqw";
            string y =
                "{\"textMedia\":[-1,-1,-1,-1,-1],\"textOperand\":[1,1,1,1,1],\"textString\":[\"\",\"\",\"\",\"\",\"\"],\"dateSite\":[-11],\"dateOperand\":[],\"dateString\":[],\"status\":[-11,0,0],\"media\":[-11,0,0],\"subItem\":true,\"context\":false,\"branchSearch\":false,\"profileIDs\":[2,5,18],\"profileViewIDs\":[48,58,38],\"currentSelectedBranch\":0}";

            SaveSearch(x, y);
        }

        [WebMethod]
        public static object SaveSearch(string name, string encodedSearch)
        {
            object response = new { };

            string x = name;
            string y = encodedSearch;

            // Why does this if statement throw an exception if both equal false?
            if (x.Trim().Equals(string.Empty) || y.Trim().Equals(string.Empty))
                throw new AjaxErrorException("Save Search", "Something went wrong", "JSFunction");


            try
            {
                {
                    return new
                    {
                        error = false,
                        name = name,
                        userID = 123,
                        date = DateTime.Now
                    };
                }
            }
            catch (Exception ex)
            {
                String e;

                if (HttpContext.Current.IsDebuggingEnabled)
                    e = ex.Message + "\n\n" + ex.StackTrace;
                else
                    e = "error error aliens approaching";

                throw new AjaxErrorException("Save Search", e, "");
            }

            return response;
        }

        public class AjaxErrorException : System.Exception
        {
            public AjaxErrorException(string title, string details, string function)
                : base(title)
            { }
            string _stackTrace;
            public override string StackTrace
            {
                get
                {
                    return _stackTrace;
                }
            }
        }
    }
}

6 个答案:

答案 0 :(得分:8)

我实际上检查过,虽然调试器步入if(throw语句)之后的语句,但它实际上并没有抛出异常。我怀疑它是IDE,IL生成和调试器之间的不一致,特别是对于throw语句。如果你尝试其他类型的声明,你实际上没有看到问题。它似乎也与这篇文章有关 If statement weirdness in Visual Studio 2008

我在下面的if块中插入了assert语句,以确保没有触发断言。

System.Diagnostics.Debug.Assert(false);

答案 1 :(得分:1)

所以我尝试将这些确切的代码粘贴到vs2008中的新控制台应用程序中并添加必要的引用。这是它为我做的:

运行应用程序似乎没有抛出任何异常。 然而,当我在调试器中运行它并跨过第30行(带有if的行)时,调试器突出显示黄色的网线(带有throw)。我能够继续踩过代码而没有抛出实际的异常。如果我在第31行(投掷线)设置断点,它永远不会被击中。

我认为调试器UI很简单。它似乎并没有真正执行该代码。我不知道会导致什么,但似乎没有什么可担心的。

答案 2 :(得分:0)

null与Empty不同。所以null.Equals(string.Empty)返回false。但null.Trim()会引发空引用异常,因此您需要首先在此代码中测试null。正如@Otavio所建议的,string.IsNullOrEmpty()是正确的方法。在检查之后不要忘记修剪字符串,因为你显然也担心只有空白字符串。

我知道它现在对你没有帮助,但.Net 4将有一个内置的方法,可以在同一个方法中执行修剪,空检查和空检查。


这应该做你需要的:

if (string.IsNullOrEmpty(x) || string.IsNullOrEmtpy(y) || x.Trim().Length == 0 || y.Trim().Length == 0)

答案 3 :(得分:0)

我刚试过这个:

    string x = "   text   \t\t\n";
    string y = "not empty";

    if (x.Trim().Equals(string.Empty) || y.Trim().Equals(string.Empty))
        Console.WriteLine("TRUE");

并没有像预期的那样打印'TRUE'。必须有一些其他错误,或者在你的实际代码中,你可以在'if'语句后面加一个分号吗?

编辑:使用异常throw而不是writeline它也不抛出。你确定这是抛出的代码吗?

答案 4 :(得分:0)

这应该可以解决问题。首先,删除项目中obj和bin目录的内容。然后重新启动计算机。

如果这不能解决问题,那么您可能需要重新安装Visual Studio。

答案 5 :(得分:-1)

您的一个/两个字符串变量可能为空,而不是空。

修改 你们为什么投票呢? 我猜他的语句会抛出NullReferenceException。

无论如何,也许我的答案不够明确:(