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

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

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

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

        如何使用 JSON.NET 序列化静态或常量成员变量?

        时间:2023-08-24
        <tfoot id='KleGr'></tfoot>

          1. <small id='KleGr'></small><noframes id='KleGr'>

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

                <legend id='KleGr'><style id='KleGr'><dir id='KleGr'><q id='KleGr'></q></dir></style></legend>
                  <bdo id='KleGr'></bdo><ul id='KleGr'></ul>
                    <tbody id='KleGr'></tbody>
                  本文介绍了如何使用 JSON.NET 序列化静态或常量成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我无法在任何地方找到答案,但是当我尝试使用静态或 const 成员变量序列化结构或类时,默认情况下它们不会序列化.如果我尝试通过设置 MemberSerialization.OptIn 来强制序列化,则会收到错误消息.

                  I haven't been able to find the answer to this anywhere, but when I try to serialize a struct or class with static or const member variables, they don't serialize by default. If I try to force serialization by setting MemberSerialization.OptIn, I get an error.

                  例如

                  [JsonObject(MemberSerialization.OptIn)]
                  public class Test
                  {    
                      [JsonProperty]
                      public int x = 1;
                  
                      [JsonProperty]
                      public static int y = 2;
                  }
                  

                  如果我尝试用以下方式序列化这个类:

                  If I try to serialize this class with:

                  Test t = new Test();
                  string s = JsonConvert.SerializeObject( t );
                  

                  我收到错误Error getting value from 'y' on 'Test'.如果 y 是 const,也会发生同样的情况.

                  I get the error Error getting value from 'y' on 'Test'. The same happens if y is const.

                  我的理论是 static 和 const 值存储在内存中的某个特殊位置,并且由于某种原因,Json 序列化程序在尝试访问它们时会死掉.不过这完全是一种预感,我在 C# 参考中看不到任何内容对于静态,这是有帮助的.我对 C# 比较陌生 - 目前这确实是一个好奇的问题.

                  My theory is that static and const values are stored somewhere special in memory, and for some reason the Json serializer dies trying to access them. That's entirely a hunch though, and I see nothing in the C# Reference for Static that's of any help. I'm relatively new to C# - and this is really a curiosity question more than anything at this point.

                  推荐答案

                  如果它愿意,它当然可以序列化静态变量.序列化是通过使用反射 API 检查对象和类型来完成的,这些 API 允许您做任何事情"——这些值无法序列化没有技术原因.

                  It could certainly serialize the static variable if it wanted to. Serialization is done by inspecting objects and types with the Reflection APIs, and those APIs allow you to do "anything" -- there is no technical reason these values cannot be serialized.

                  但是,默认情况下不支持此功能有一个合乎逻辑的理由:它没有多大意义.您正在序列化一个 instance,而 staticconst 成员在逻辑上不是实例的一部分,而是整个类的一部分.

                  There is, however, a logical reason not to support this by default: it doesn't make much sense. You are serializing an instance, and static or const members are not logically part of an instance but of the class as a whole.

                  也就是说,你仍然可以序列化 static 成员,如果它是一个属性:

                  That said, you can still serialize static member if it's a property:

                  [JsonProperty]
                  public static int y { get; set; } // this will be serialized
                  

                  当然,您可以通过创建自定义 JsonConverter 来完全覆盖序列化程序的行为.

                  And of course you can completely override the serializer's behavior by creating a custom JsonConverter.

                  这篇关于如何使用 JSON.NET 序列化静态或常量成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Newtonsoft Json 将字典反序列化为来自 DataContractJs 下一篇:如何告诉 Json.NET 忽略第 3 方对象中的属性?

                  相关文章

                  最新文章

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

                      • <bdo id='5HcJL'></bdo><ul id='5HcJL'></ul>
                    1. <small id='5HcJL'></small><noframes id='5HcJL'>