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

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

  • <legend id='x2hTF'><style id='x2hTF'><dir id='x2hTF'><q id='x2hTF'></q></dir></style></legend>
      <bdo id='x2hTF'></bdo><ul id='x2hTF'></ul>

      1. Json.NET 序列化私有成员而不是只读属性

        时间:2023-08-23

          <tbody id='ayyNC'></tbody>
              • <bdo id='ayyNC'></bdo><ul id='ayyNC'></ul>
                <legend id='ayyNC'><style id='ayyNC'><dir id='ayyNC'><q id='ayyNC'></q></dir></style></legend>

                • <tfoot id='ayyNC'></tfoot>

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

                • <i id='ayyNC'><tr id='ayyNC'><dt id='ayyNC'><q id='ayyNC'><span id='ayyNC'><b id='ayyNC'><form id='ayyNC'><ins id='ayyNC'></ins><ul id='ayyNC'></ul><sub id='ayyNC'></sub></form><legend id='ayyNC'></legend><bdo id='ayyNC'><pre id='ayyNC'><center id='ayyNC'></center></pre></bdo></b><th id='ayyNC'></th></span></q></dt></tr></i><div id='ayyNC'><tfoot id='ayyNC'></tfoot><dl id='ayyNC'><fieldset id='ayyNC'></fieldset></dl></div>
                  本文介绍了Json.NET 序列化私有成员而不是只读属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何自定义 Json.NET 以序列化私有成员而不序列化公共只读属性(不使用属性).

                  How can I customize Json.NET to serialize private members and NOT serialize public readonly properties (without using attributes).

                  我曾尝试过创建自定义 IContractResolver,但有点迷茫.

                  I've had a stab around at creating a custom IContractResolver but am a bit lost.

                  推荐答案

                  对于部分答案,使用 DefaultContractResolver.DefaultMembersSearchFlags 可以让它包含私有的东西:

                  For a partial answer, messing with DefaultContractResolver.DefaultMembersSearchFlags can get it to include private things:

                  Newtonsoft.Json.JsonSerializerSettings jss = new Newtonsoft.Json.JsonSerializerSettings();
                  
                  if (includePrivateMembers)
                  {
                      Newtonsoft.Json.Serialization.DefaultContractResolver dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                      dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
                      jss.ContractResolver = dcr;
                  }
                  
                  return Newtonsoft.Json.JsonConvert.SerializeObject(o, jss);
                  

                  似乎对很多对象都有效,尽管其中一些似乎会产生 CLR 异常.

                  Seems to work on a lot of objects, though with some this seems to generate a CLR exception.

                  这篇关于Json.NET 序列化私有成员而不是只读属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:json.net 不序列化派生类的属性 下一篇:Json.net 反序列化 null guid 案例

                  相关文章

                  最新文章

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

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

                      <tfoot id='Ya78h'></tfoot>
                        <bdo id='Ya78h'></bdo><ul id='Ya78h'></ul>