久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <i id='zAMZz'><tr id='zAMZz'><dt id='zAMZz'><q id='zAMZz'><span id='zAMZz'><b id='zAMZz'><form id='zAMZz'><ins id='zAMZz'></ins><ul id='zAMZz'></ul><sub id='zAMZz'></sub></form><legend id='zAMZz'></legend><bdo id='zAMZz'><pre id='zAMZz'><center id='zAMZz'></center></pre></bdo></b><th id='zAMZz'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='zAMZz'><tfoot id='zAMZz'></tfoot><dl id='zAMZz'><fieldset id='zAMZz'></fieldset></dl></div>
  1. <tfoot id='zAMZz'></tfoot><legend id='zAMZz'><style id='zAMZz'><dir id='zAMZz'><q id='zAMZz'></q></dir></style></legend>
  2. <small id='zAMZz'></small><noframes id='zAMZz'>

        <bdo id='zAMZz'></bdo><ul id='zAMZz'></ul>

    1. 為動態對象的屬性返回 null 的空合并運算符

      Null-coalescing operator returning null for properties of dynamic objects(為動態對象的屬性返回 null 的空合并運算符)
        <bdo id='f5khx'></bdo><ul id='f5khx'></ul>

            • <i id='f5khx'><tr id='f5khx'><dt id='f5khx'><q id='f5khx'><span id='f5khx'><b id='f5khx'><form id='f5khx'><ins id='f5khx'></ins><ul id='f5khx'></ul><sub id='f5khx'></sub></form><legend id='f5khx'></legend><bdo id='f5khx'><pre id='f5khx'><center id='f5khx'></center></pre></bdo></b><th id='f5khx'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='f5khx'><tfoot id='f5khx'></tfoot><dl id='f5khx'><fieldset id='f5khx'></fieldset></dl></div>
                <tbody id='f5khx'></tbody>
              • <tfoot id='f5khx'></tfoot>

                <small id='f5khx'></small><noframes id='f5khx'>

                <legend id='f5khx'><style id='f5khx'><dir id='f5khx'><q id='f5khx'></q></dir></style></legend>
                本文介紹了為動態對象的屬性返回 null 的空合并運算符的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我最近在使用 Json.NET 將 JSON 解析為動態對象時發現了 null 合并運算符的問題.假設這是我的動態對象:

                I have recently found a problem with the null-coalescing operator while using Json.NET to parse JSON as dynamic objects. Suppose this is my dynamic object:

                string json = "{ "phones": { "personal": null }, "birthday": null }";
                dynamic d = JsonConvert.DeserializeObject(json);
                

                如果我嘗試使用 ??d 的字段之一上的運算符,它返回 null:

                If I try to use the ?? operator on one of the field of d, it returns null:

                string s = "";
                s += (d.phones.personal ?? "default");
                Console.WriteLine(s + " " + s.Length); //outputs  0
                

                但是,如果我將動態屬性分配給字符串,則它可以正常工作:

                However, if I assign a the dynamic property to a string, then it works fine:

                string ss = d.phones.personal;
                string s = "";
                s += (ss ?? "default");
                Console.WriteLine(s + " " + s.Length); //outputs default 7
                

                最后,當我輸出 Console.WriteLine(d.phones.personal == null) 時,它輸出 True.

                Finally, when I output Console.WriteLine(d.phones.personal == null) it outputs True.

                我在 Pastebin 上對這些問題進行了廣泛的測試.

                I have made an extensive test of these issues on Pastebin.

                推薦答案

                這是由于 Json.NET 和 ?? 運算符的晦澀行為造成的.

                This is due to obscure behaviors of Json.NET and the ?? operator.

                首先,當您將 JSON 反序列化為 dynamic 對象時,實際返回的是 Linq-to-JSON 類型 JToken 的子類(例如 JObjectJValue) 的自定義實現IDynamicMetaObjectProvider.即

                Firstly, when you deserialize JSON to a dynamic object, what is actually returned is a subclass of the Linq-to-JSON type JToken (e.g. JObject or JValue) which has a custom implementation of IDynamicMetaObjectProvider. I.e.

                dynamic d1 = JsonConvert.DeserializeObject(json);
                var d2 = JsonConvert.DeserializeObject<JObject>(json);
                

                實際上返回的是相同的東西.所以,對于你的 JSON 字符串,如果我這樣做了

                Are actually returning the same thing. So, for your JSON string, if I do

                    var s1 = JsonConvert.DeserializeObject<JObject>(json)["phones"]["personal"];
                    var s2 = JsonConvert.DeserializeObject<dynamic>(json).phones.personal;
                

                這兩個表達式都返回完全相同的動態對象.但是返回的是什么對象?這讓我們看到了 Json.NET 的第二個模糊行為:不是用 null 指針表示空值,而是用一個特殊的 JValueJValue.Type 等于 JTokenType.Null.因此,如果我這樣做:

                Both these expressions evaluate to exactly the same returned dynamic object. But what object is returned? That gets us to the second obscure behavior of Json.NET: rather than representing null values with null pointers, it represents then with a special JValue with JValue.Type equal to JTokenType.Null. Thus if I do:

                    WriteTypeAndValue(s1, "s1");
                    WriteTypeAndValue(s2, "s2");
                

                控制臺輸出為:

                "s1":  Newtonsoft.Json.Linq.JValue: ""
                "s2":  Newtonsoft.Json.Linq.JValue: ""
                

                即這些對象不為空,它們被分配了 POCO,它們的 ToString() 返回一個空字符串.

                I.e. these objects are not null, they are allocated POCOs, and their ToString() returns an empty string.

                但是,當我們將動態類型分配給字符串時會發生什么?

                But, what happens when we assign that dynamic type to a string?

                    string tmp;
                    WriteTypeAndValue(tmp = s2, "tmp = s2");
                

                打印:

                "tmp = s2":  System.String: null value
                

                為什么不一樣?這是因為 DynamicMetaObjectJValue 解決動態類型到字符串的轉換最終調用 ConvertUtils.Convert(value, CultureInfo.InvariantCulture, binder.Type) 最終返回 null 用于 JTokenType.Null 值,這與顯式轉換為字符串執行的邏輯相同,避免所有使用 dynamic:

                Why the difference? It is because the DynamicMetaObject returned by JValue to resolve the conversion of the dynamic type to string eventually calls ConvertUtils.Convert(value, CultureInfo.InvariantCulture, binder.Type) which eventually returns null for a JTokenType.Null value, which is the same logic performed by the explicit cast to string avoiding all uses of dynamic:

                    WriteTypeAndValue((string)JsonConvert.DeserializeObject<JObject>(json)["phones"]["personal"], "Linq-to-JSON with cast");
                    // Prints "Linq-to-JSON with cast":  System.String: null value
                
                    WriteTypeAndValue(JsonConvert.DeserializeObject<JObject>(json)["phones"]["personal"], "Linq-to-JSON without cast");     
                    // Prints "Linq-to-JSON without cast":  Newtonsoft.Json.Linq.JValue: ""
                

                現在,進入實際問題.正如 husterk 所指出的??當兩個操作數之一是 dynamic 時,operator 返回 dynamic,所以 d.phones.personal ??"default" 不會嘗試執行類型轉換,因此返回的是 JValue:

                Now, to the actual question. As husterk noted the ?? operator returns dynamic when one of the two operands is dynamic, so d.phones.personal ?? "default" does not attempt to perform a type conversion, thus the return is a JValue:

                    dynamic d = JsonConvert.DeserializeObject<dynamic>(json);
                    WriteTypeAndValue((d.phones.personal ?? "default"), "d.phones.personal ?? "default"");
                    // Prints "(d.phones.personal ?? "default")":  Newtonsoft.Json.Linq.JValue: ""
                

                但是,如果我們通過將動態返回分配給字符串來調用 Json.NET 到字符串的類型轉換,那么轉換器將在合并運算符完成其工作并返回一個非-null JValue:

                But if we invoke Json.NET's type conversion to string by assigning the dynamic return to a string, then the converter will kick in and return an actual null pointer after the coalescing operator has done its work and returned a non-null JValue:

                    string tmp;
                    WriteTypeAndValue(tmp = (d.phones.personal ?? "default"), "tmp = (d.phones.personal ?? "default")");
                    // Prints "tmp = (d.phones.personal ?? "default")":  System.String: null value
                

                這解釋了您所看到的差異.

                This explains the difference you are seeing.

                為避免這種行為,請在應用合并運算符之前強制從動態轉換為字符串:

                To avoid this behavior, force the conversion from dynamic to string before the coalescing operator is applied:

                s += ((string)d.phones.personal ?? "default");
                

                最后,將類型和值寫入控制臺的輔助方法:

                Finally, the helper method to write the type and value to the console:

                public static void WriteTypeAndValue<T>(T value, string prefix = null)
                {
                    prefix = string.IsNullOrEmpty(prefix) ? null : """+prefix+"": ";
                
                    Type type;
                    try
                    {
                        type = value.GetType();
                    }
                    catch (NullReferenceException)
                    {
                        Console.WriteLine(string.Format("{0} {1}: null value", prefix, typeof(T).FullName));
                        return;
                    }
                    Console.WriteLine(string.Format("{0} {1}: "{2}"", prefix, type.FullName, value));
                }
                

                (順便說一句,空類型 JValue 的存在解釋了表達式 (object)(JValue)(string)null == (object)(JValue)null 可能評估為 false).

                (As an aside, the existence of the null-type JValue explains how the expression (object)(JValue)(string)null == (object)(JValue)null might possibly evaluate to false).

                這篇關于為動態對象的屬性返回 null 的空合并運算符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                Parse malformed XML(解析格式錯誤的 XML)
                <i id='WUHdk'><tr id='WUHdk'><dt id='WUHdk'><q id='WUHdk'><span id='WUHdk'><b id='WUHdk'><form id='WUHdk'><ins id='WUHdk'></ins><ul id='WUHdk'></ul><sub id='WUHdk'></sub></form><legend id='WUHdk'></legend><bdo id='WUHdk'><pre id='WUHdk'><center id='WUHdk'></center></pre></bdo></b><th id='WUHdk'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='WUHdk'><tfoot id='WUHdk'></tfoot><dl id='WUHdk'><fieldset id='WUHdk'></fieldset></dl></div>
                <legend id='WUHdk'><style id='WUHdk'><dir id='WUHdk'><q id='WUHdk'></q></dir></style></legend>

                <small id='WUHdk'></small><noframes id='WUHdk'>

                • <bdo id='WUHdk'></bdo><ul id='WUHdk'></ul>

                      <tbody id='WUHdk'></tbody>

                          <tfoot id='WUHdk'></tfoot>

                        • 主站蜘蛛池模板: 中文字幕综合 | 免费看国产一级特黄aaaa大片 | 精品香蕉一区二区三区 | 91精品一区 | 亚洲精品国产综合区久久久久久久 | 成人在线观看亚洲 | 一级黄色片免费在线观看 | 超碰在线人 | 欧美日韩在线视频一区 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 成人国产精品色哟哟 | 91精品国产自产精品男人的天堂 | 成人污污视频 | 欧美在线观看免费观看视频 | 精品国产乱码久久久久久88av | 日韩精品在线视频 | 国产精品视频www | 国产一级片一区二区 | 日韩美女爱爱 | 91精品久久久久久久久中文字幕 | 日韩视频一区在线观看 | 国产精品视频一二三区 | 国产在线观看av | 日韩精品久久一区二区三区 | 精品国产一区二区久久 | 久久狠狠| 美女网站视频免费黄 | 丁香久久| 亚洲社区在线 | 国产在线观看一区二区三区 | 一区日韩 | 午夜精品久久久 | 91社影院在线观看 | 免费黄网站在线观看 | 国产欧美在线 | 91 久久 | 91久久| 亚洲国产精品一区二区三区 | 日韩精品一区二 | 色婷婷av99xx | 国产1区2区在线观看 |