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

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

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

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

      json.net 不序列化派生类的属性

      时间:2023-08-23
      <tfoot id='MiOfO'></tfoot>

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

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

                本文介绍了json.net 不序列化派生类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在使用 JSON.NET 6.0.1.当我使用 SerializeObject 方法序列化派生类的对象时,它仅序列化基类的属性.以下是代码片段:

                I'm using JSON.NET 6.0.1. When I use the SerializeObject method to serialize an object of my derived class, it serializes properties from base class only. Here is the code snippet:

                string v = JsonConvert.SerializeObject(
                                service, 
                                Formatting.Indented, 
                                new JsonSerializerSettings()
                                {
                                    TypeNameHandling = TypeNameHandling.All
                                });
                

                基类:

                [DataContract]
                public abstract partial class DataEntity : IDataEntity, INotifyPropertyChanging, INotifyPropertyChanged
                {
                    ...
                }
                

                派生类:

                [Table(Name = "dbo.mytable")]
                public sealed class mytable : DataEntity
                {
                    ...
                }
                

                我错过了什么吗?

                推荐答案

                是的,您缺少派生类的 [DataContract] 属性.您还需要将 [DataMember] 添加到您想要序列化的任何属性或字段(如果您尚未添加它们).Json.Net 在 5.0 版本 1 中进行了更改(4 月2013)这样 [DataContract] 属性不会被继承.

                Yes, you are missing the [DataContract] attribute on the derived class. You also need to add [DataMember] to any properties or fields that you want serialized, if you haven't already added them. Json.Net was changed in version 5.0 release 1 (April 2013) such that the [DataContract] attribute is not inherited.

                请注意,如果您从您的类中删除所有 [DataContract][DataMemeber] 实例,Json.Net 的行为会有所不同:在在这种情况下,Json.Net 的默认行为是序列化所有公共属性,包括基类和派生类.

                Note that if you remove all instances of [DataContract] and [DataMemeber] from your classes, Json.Net behaves differently: in that case, the default behavior is for Json.Net to serialize all public properties, both in the base and derived classes.

                这篇关于json.net 不序列化派生类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:将 JSON.NET JObject 的属性/标记转换为字典键 下一篇:Json.NET 序列化私有成员而不是只读属性

                相关文章

                最新文章

                1. <tfoot id='xDzJO'></tfoot>
                    <legend id='xDzJO'><style id='xDzJO'><dir id='xDzJO'><q id='xDzJO'></q></dir></style></legend>
                      <bdo id='xDzJO'></bdo><ul id='xDzJO'></ul>

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

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