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

    <tfoot id='TMtUw'></tfoot>

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

      1. <legend id='TMtUw'><style id='TMtUw'><dir id='TMtUw'><q id='TMtUw'></q></dir></style></legend>

      2. <i id='TMtUw'><tr id='TMtUw'><dt id='TMtUw'><q id='TMtUw'><span id='TMtUw'><b id='TMtUw'><form id='TMtUw'><ins id='TMtUw'></ins><ul id='TMtUw'></ul><sub id='TMtUw'></sub></form><legend id='TMtUw'></legend><bdo id='TMtUw'><pre id='TMtUw'><center id='TMtUw'></center></pre></bdo></b><th id='TMtUw'></th></span></q></dt></tr></i><div id='TMtUw'><tfoot id='TMtUw'></tfoot><dl id='TMtUw'><fieldset id='TMtUw'></fieldset></dl></div>

        向 json.net 添加自定义属性

        时间:2023-07-27
      3. <tfoot id='yyIYB'></tfoot>

          <legend id='yyIYB'><style id='yyIYB'><dir id='yyIYB'><q id='yyIYB'></q></dir></style></legend>
            • <bdo id='yyIYB'></bdo><ul id='yyIYB'></ul>
              1. <small id='yyIYB'></small><noframes id='yyIYB'>

                <i id='yyIYB'><tr id='yyIYB'><dt id='yyIYB'><q id='yyIYB'><span id='yyIYB'><b id='yyIYB'><form id='yyIYB'><ins id='yyIYB'></ins><ul id='yyIYB'></ul><sub id='yyIYB'></sub></form><legend id='yyIYB'></legend><bdo id='yyIYB'><pre id='yyIYB'><center id='yyIYB'></center></pre></bdo></b><th id='yyIYB'></th></span></q></dt></tr></i><div id='yyIYB'><tfoot id='yyIYB'></tfoot><dl id='yyIYB'><fieldset id='yyIYB'></fieldset></dl></div>
                  <tbody id='yyIYB'></tbody>
                • 本文介绍了向 json.net 添加自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  JSON.NET 带有 [JsonIgnore][JsonProperty] 等属性属性.

                  JSON.NET comes with property attributes like [JsonIgnore] and [JsonProperty].

                  我想创建一些在序列化运行时运行的自定义,例如[JsonIgnoreSerialize] 或 [JsonIgnoreDeserialize]

                  I want to create some custom ones that get run when the serialisation runs e.g. [JsonIgnoreSerialize] or [JsonIgnoreDeserialize]

                  我将如何扩展框架以包含它?

                  How would I go about extending the framework to include this?

                  推荐答案

                  你可以像这样写一个自定义的合约解析器

                  You can write a custom contract resolver like this

                  public class MyContractResolver<T> : Newtonsoft.Json.Serialization.DefaultContractResolver 
                                                          where T : Attribute
                  {
                      Type _AttributeToIgnore = null;
                  
                      public MyContractResolver()
                      {
                          _AttributeToIgnore = typeof(T);
                      }
                  
                      protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
                      {
                          var list =  type.GetProperties()
                                      .Where(x => !x.GetCustomAttributes().Any(a => a.GetType() == _AttributeToIgnore))
                                      .Select(p => new JsonProperty()
                                      {
                                          PropertyName = p.Name,
                                          PropertyType = p.PropertyType,
                                          Readable = true,
                                          Writable = true,
                                          ValueProvider = base.CreateMemberValueProvider(p)
                                      }).ToList();
                  
                          return list;
                      }
                  }
                  

                  你可以在序列化/反序列化中使用它

                  You can use it in serialization/deserialization like

                  var json = JsonConvert.SerializeObject(
                              obj, 
                              new JsonSerializerSettings() {
                                  ContractResolver = new MyContractResolver<JsonIgnoreSerialize>()
                              });
                  
                  var obj = JsonConvert.DeserializeObject<SomeType>(
                              json, 
                              new JsonSerializerSettings() {
                                  ContractResolver = new MyContractResolver<JsonIgnoreDeserialize>()
                              });
                  

                  这篇关于向 json.net 添加自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:NonSerialized 和 Xml.Serialization.XmlIgnore 之间的区别? 下一篇:自定义 .NET 属性的实际使用

                  相关文章

                • <small id='kq261'></small><noframes id='kq261'>

                  <legend id='kq261'><style id='kq261'><dir id='kq261'><q id='kq261'></q></dir></style></legend>

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