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

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

      <small id='7wxxl'></small><noframes id='7wxxl'>

    1. <tfoot id='7wxxl'></tfoot>

      • <bdo id='7wxxl'></bdo><ul id='7wxxl'></ul>
    2. <legend id='7wxxl'><style id='7wxxl'><dir id='7wxxl'><q id='7wxxl'></q></dir></style></legend>

      覆蓋自定義 JSON.net 合同解析器中的屬性值

      Overriding a property value in custom JSON.net contract resolver(覆蓋自定義 JSON.net 合同解析器中的屬性值)
    3. <i id='ESafV'><tr id='ESafV'><dt id='ESafV'><q id='ESafV'><span id='ESafV'><b id='ESafV'><form id='ESafV'><ins id='ESafV'></ins><ul id='ESafV'></ul><sub id='ESafV'></sub></form><legend id='ESafV'></legend><bdo id='ESafV'><pre id='ESafV'><center id='ESafV'></center></pre></bdo></b><th id='ESafV'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ESafV'><tfoot id='ESafV'></tfoot><dl id='ESafV'><fieldset id='ESafV'></fieldset></dl></div>
      <tfoot id='ESafV'></tfoot>

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

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

                  <tbody id='ESafV'></tbody>
                <legend id='ESafV'><style id='ESafV'><dir id='ESafV'><q id='ESafV'></q></dir></style></legend>

                本文介紹了覆蓋自定義 JSON.net 合同解析器中的屬性值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在嘗試實現一個自定義 JSON.net IContractResolver,它將用指定的字符串替換所有空屬性值.我知道此功能可通過序列化類型成員的屬性獲得;這是我們正在考慮的替代路線.

                I am attempting to implement a custom JSON.net IContractResolver that will replace all null property values with a specified string. I'm aware that this functionality is available via attributes on member of types that get serialized; this is an alternative route that we're considering.

                到目前為止,我的解析器實現如下.StringValueProvider 是 IValueProvider 的一個簡單實現,不會影響問題,因為我不知道如何獲取 property 的值,因為我不知道實例的這種方法提供了 member 所以我不能將它作為參數傳遞給 GetValue() (在代碼示例中標記為 WHAT-GOES-HERE?).

                My resolver implementation so far is as follows. StringValueProvider is a simple implementation of IValueProvider that doesn't affect the problem, which is that I can't figure out how to get the value of property as I have no knowledge in this method of the instance that supplied member so I can't pass it in as an argument to GetValue() (marked as WHAT-GOES-HERE? in the code sample).

                有沒有一種方法可以讓我從 memberproperty 獲得我需要的東西?

                Is there a way that I can get what I need from member or from property?

                public class NullSubstitutionPropertyValueResolver : DefaultContractResolver
                {
                    private readonly string _substitutionValue;
                
                    public NullSubstitutionPropertyValueResolver(string substitutionValue)
                    {
                        _substitutionValue = substitutionValue;
                    }
                
                    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
                    {
                        JsonProperty result = base.CreateProperty(member, memberSerialization);
                
                        PropertyInfo property = member as PropertyInfo;
                
                        if (property == null)
                        {
                            return result;
                        }
                
                        // What do I use here to get the property value?
                        bool isNull = property.GetValue(WHAT-GOES-HERE?) == null;
                
                        if (isNull)
                        {
                            result.ValueProvider = new StringValueProvider(_substitutionValue);
                        }
                
                        return result;
                    }
                }
                

                推薦答案

                合約解析器不關心實例,它關心類型.值提供者關注實例.在合約解析器中,您根據屬性類型決定是否應將值提供程序應用于屬性(例如,您可能只想在 string 上使用 StringValueProvider屬性?)然后,您讓值提供者存儲對該屬性的引用(將其與替換值一起傳遞到構造函數中).在值提供者中,您可以從對象實例中讀取值,檢查它是否為空并進行適當的值替換.

                The contract resolver is not concerned with instances, it is concerned with types. The value provider is concerned with instances. In the contract resolver, you decide whether the value provider should be applied to the property based on the property type (for example, maybe you only want to use a StringValueProvider on string properties?) Then, you make the value provider store a reference to the property (pass it in the constructor along with the substitution value). In the value provider, you can read the value from the object instance, check if it is null and do the appropriate value substitution.

                代碼應如下所示:

                public class NullSubstitutionPropertyValueResolver : DefaultContractResolver
                {
                    private readonly string _substitutionValue;
                
                    public NullSubstitutionPropertyValueResolver(string substitutionValue)
                    {
                        _substitutionValue = substitutionValue;
                    }
                
                    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
                    {
                        JsonProperty result = base.CreateProperty(member, memberSerialization);
                
                        PropertyInfo property = member as PropertyInfo;
                
                        if (property.PropertyType == typeof(string))
                        {
                            result.ValueProvider = new StringValueProvider(property, _substitutionValue);
                        }
                
                        return result;
                    }
                }
                
                public class StringValueProvider : IValueProvider
                {
                    private PropertyInfo _targetProperty;
                    private string _substitutionValue;
                
                    public StringValueProvider(PropertyInfo targetProperty, string substitutionValue)
                    {
                        _targetProperty = targetProperty;
                        _substitutionValue = substitutionValue;
                    }
                
                    // SetValue gets called by Json.Net during deserialization.
                    // The value parameter has the original value read from the JSON;
                    // target is the object on which to set the value.
                    public void SetValue(object target, object value)
                    {
                        _targetProperty.SetValue(target, value);
                    }
                
                    // GetValue is called by Json.Net during serialization.
                    // The target parameter has the object from which to read the value;
                    // the return value is what gets written to the JSON
                    public object GetValue(object target)
                    {
                        object value = _targetProperty.GetValue(target);
                        return value == null ? _substitutionValue : value;
                    }
                }
                

                這是一個工作演示:https://dotnetfiddle.net/PAZULK

                這篇關于覆蓋自定義 JSON.net 合同解析器中的屬性值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)

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

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

                          主站蜘蛛池模板: 亚洲男人天堂 | 欧美一级欧美三级在线观看 | 日本精品一区二区三区视频 | 免费在线一区二区三区 | 91久久国产综合久久 | 国产精品综合视频 | 中文字幕视频在线 | 9999久久| 成人精品一区二区三区中文字幕 | 国产精品久久久久久婷婷天堂 | 久久精品一二三影院 | 成人福利网站 | 日韩1区| 欧美精品在线看 | 综合久久综合久久 | 香蕉超碰| 国产精品久久久久婷婷二区次 | 精品一区二区三区四区在线 | 国产一级片精品 | 成人在线观看免费视频 | 超碰在线免费公开 | 成人一区二区三区在线 | 懂色中文一区二区三区在线视频 | 一区二区三区国产精品 | 国产一区二区影院 | 在线视频a | 国产福利视频 | 国产激情一区二区三区 | 欧美 日韩 国产 成人 在线 | 男女视频在线观看免费 | 女人精96xxx免费网站p | av在线播放网站 | 成人婷婷 | 午夜影院在线观看免费 | 亚洲巨乳自拍在线视频 | 国产日韩一区二区三免费高清 | 成人看片在线观看 | 精品久久久久久久久久 | 精品一区电影 | 国产精品久久网 | 国外成人在线视频网站 |