【问题标题】:HttpContext.Current.Session unclear behaviour booleanHttpContext.Current.Session 不明确行为布尔值
【发布时间】:2017-01-02 21:04:17
【问题描述】:

我在尝试获取存储在 HttpContext.Current.Session 的布尔属性的值时遇到了一种奇怪的行为。

对象是一个布尔值。首先,我尝试检查对象是否存在,如果存在,请使用它。

我试图在?: 运算符中使用它,但它的行为很奇怪。这是我的监视窗口:

前提

  • "ExistingKey" 键存在且值为 false(如果键 不存在返回 false)。

结果

  1. 检查 !=null 是否返回 false(第一件事很奇怪)。
  2. 使用?: 运算符时,除了条件为假外,它还返回第一个表达式,4(第二个奇怪的地方)。

有人能解释一下这种行为吗?


注意:我并不是要寻找绕过这种情况的替代方法。只是问为什么会这样。

【问题讨论】:

  • 好吧,我测试了它,HttpContext.Current.Session["ExistingKey"] != null 实际上返回 true。我也很好奇这种行为。
  • 对我来说看起来是正确的。 ExistingKey 有值;它不为空。你所有的比较都反映了这一点。那么问题是什么?
  • @JeffSiver HttpContext.Current.Session["ExistingKey"] != null 返回 false 但它应该为 true。在我的测试中,顺便说一句,这也是正确的。我怀疑这与当前上下文有关,但无法确认。也许以前遇到过这个问题的人可以提供帮助
  • @uteist,我对你的监视窗口感到困惑。最后两项,带有? 4 : 5 是正确的。第三项是否有错字导致其显示不同?
  • @GiladGreen - 为什么第 4 行不正确?唯一不适合的是第 3 行。所有其他的都是一致的。

标签: c# asp.net session httpcontext conditional-operator


【解决方案1】:

目前请不要将此视为答案,由于空间和格式限制,以下内容在答案中比在评论中更容易写。

我同意 cmets 的问题,第 3 行与其他行的结果不一致。我能想到的唯一可以解释这一点的是 Visual Studio 中的 Watch 窗口有过时的数据/有一个损坏的状态。我认为执行相同的语句但在代码本身可以证明或反驳这一点。以下代码与您所拥有的相同,但输出到StringBuilder。您能否执行此操作并发布结果字符串,如果这与您在 Watch 窗口中的内容有什么不同,请告诉我们?

var session = HttpContext.Current.Session;
var builder = new System.Text.StringBuilder();

builder.AppendFormat("session[\"someKeyThatDoesNotExist\"] => value {0}", session["someKeyThatDoesNotExist"] ?? "null").AppendLine();
builder.AppendFormat("session[\"ExistingKey\"] => value {0}", session["ExistingKey"] ?? "null").AppendLine();
builder.AppendFormat("session[\"ExistingKey\"] != null => value {0}", session["ExistingKey"] != null).AppendLine();
builder.AppendFormat("session[\"ExistingKey\"] != null ? 4 : 5 => value {0}", session["ExistingKey"] != null ? 4 : 5).AppendLine();
builder.AppendFormat("session[\"ExistingKey\"] == null ? 4 : 5 => value {0}", session["ExistingKey"] == null ? 4 : 5).AppendLine();

var totalDebugInfo = builder.ToString();

【讨论】:

    【解决方案2】:

    这似乎是监视窗口中的某种错误。我测试了以下代码:

    protected void Page_Load(object sender, EventArgs e)
    {
       var  objDict = new Dictionary<string, object>();
        var boolDict = new Dictionary<string, bool>();
        Session["ExistingValue"] = false;
        bool? nullableValue = false;
    
        Session["ExistingValueNullable"] = nullableValue;
        var existingValue = (bool)Session["ExistingValue"];
        var existingValueIsNotNull = Session["existingValue"] != null;
        objDict["ExistingValue"] = false;
        boolDict["ExistingValue"] = false;
        bool existingValueIsNotNullIf = false;
        if (Session["ExistingValue"] != null)
        {
            existingValueIsNotNullIf = true;
        }
    }
    

    我在监视窗口中得到以下信息:

    因此您可以看到,在 Session 和 Dictionary 的情况下,!= null 的计算结果为 false。 Dictionary 会正确评估此比较。

    奇怪的是,'Session["ExistingValue"] != null' 和 'Session["ExistingValue"]' == null' 都是假的。

    如果我先将会话值转换为布尔值,然后与 null 进行比较,我会得到正确的结果。

    最后,如果我在代码中测试值 'Session["ExistingValue"] != null',我会得到正确的结果。这至少让我放心,这是监视窗口中的内容,代码中不应该有类似的问题。

    我所有的测试都是在 Visual Studio 2015 中进行的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 2019-11-14
      • 2019-06-08
      相关资源
      最近更新 更多