久久久久久久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>

                          主站蜘蛛池模板: 伊人伊成久久人综合网站 | 欧美日韩国产一区二区三区 | 成人二区三区 | 久久久久网站 | 久久精品国产一区 | 国产欧美一区二区三区在线看 | 日本aaa视频 | 91精品国产综合久久久久久丝袜 | 亚洲三区在线观看 | 一区二区日韩 | 日日干夜夜操 | 中文字幕亚洲欧美 | 免费在线一区二区三区 | 黑人巨大精品欧美一区二区免费 | 狠狠热视频 | www.99热 | 欧美精品一区二区在线观看 | 久草网站 | 亚洲国产精品一区二区www | 亚洲国产精品久久 | 亚洲自拍偷拍免费视频 | 女同久久另类99精品国产 | 欧美成人在线免费 | 超碰精品在线 | 玖玖综合在线 | 久久久久久免费免费 | 国产一区高清 | 日日碰碰 | 国产精品久久一区二区三区 | 一区二区精品在线 | 免费一级片 | 亚洲色图插插插 | 久久久久国产 | 中文字幕一区在线观看视频 | 精品久久久久久亚洲精品 | 视频一二三区 | 欧美综合一区 | 91看片免费版 | 国产精品3区 | 精品一区精品二区 | 国产电影一区二区三区爱妃记 |