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

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

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

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

      使用 JSON.net,如何防止在基类上下文中使用派生

      时间:2023-08-23

      <legend id='IcCie'><style id='IcCie'><dir id='IcCie'><q id='IcCie'></q></dir></style></legend>
          <tbody id='IcCie'></tbody>
          <tfoot id='IcCie'></tfoot>

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

            • <bdo id='IcCie'></bdo><ul id='IcCie'></ul>
              <i id='IcCie'><tr id='IcCie'><dt id='IcCie'><q id='IcCie'><span id='IcCie'><b id='IcCie'><form id='IcCie'><ins id='IcCie'></ins><ul id='IcCie'></ul><sub id='IcCie'></sub></form><legend id='IcCie'></legend><bdo id='IcCie'><pre id='IcCie'><center id='IcCie'></center></pre></bdo></b><th id='IcCie'></th></span></q></dt></tr></i><div id='IcCie'><tfoot id='IcCie'></tfoot><dl id='IcCie'><fieldset id='IcCie'></fieldset></dl></div>
                本文介绍了使用 JSON.net,如何防止在基类上下文中使用派生类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                给定一个数据模型:

                [DataContract]
                public class Parent
                {
                    [DataMember]
                    public IEnumerable<ChildId> Children { get; set; }
                }
                
                [DataContract]
                public class ChildId
                {
                    [DataMember]
                    public string Id { get; set; }
                }
                
                [DataContract]
                public class ChildDetail : ChildId
                {
                    [DataMember]
                    public string Name { get; set; }
                }
                

                出于实现方便的原因,有时 Parent 上的 ChildId 对象实际上是 ChildDetail 对象.当我使用 JSON.net 序列化 Parent 时,它们会与所有 ChildDetail 属性一起写出.

                For implementation convenience reasons, there are times when the ChildId objects on the Parent are in fact ChildDetail objects. When I use JSON.net to serialize the Parent, they are written out with all of the ChildDetail properties.

                是否有任何方法可以指示 JSON.net(或任何其他 JSON 序列化程序,我对项目的投入还不够远)在序列化为基类时忽略派生类属性?

                Is there any way to instruct JSON.net (or any other JSON serializer, I'm not far enough into the project to be committed to one) to ignore derived class properties when serializing as a base class?

                重要的是,当我直接序列化派生类时,我能够生成所有属性.我只想抑制 Parent 对象中的多态性.

                It is important that when I serialize the derived class directly that I'm able to produce all the properties. I only want to inhibit the polymorphism in the Parent object.

                推荐答案

                我使用自定义 Contract Resolver 来限制我的哪些属性要序列化.这可能会为您指明正确的方向.

                I use a custom Contract Resolver to limit which of my properties to serialize. This might point you in the right direction.

                例如

                /// <summary>
                /// json.net serializes ALL properties of a class by default
                /// this class will tell json.net to only serialize properties if they MATCH 
                /// the list of valid columns passed through the querystring to criteria object
                /// </summary>
                public class CriteriaContractResolver<T> : DefaultContractResolver
                {
                    List<string> _properties;
                
                    public CriteriaContractResolver(List<string> properties)
                    {
                        _properties = properties
                    }
                
                    protected override IList<JsonProperty> CreateProperties(
                        JsonObjectContract contract)
                    {
                        IList<JsonProperty> filtered = new List<JsonProperty>();
                
                        foreach (JsonProperty p in base.CreateProperties(contract))
                            if(_properties.Contains(p.PropertyName)) 
                                filtered.Add(p);
                
                        return filtered;
                    }
                }
                

                在覆盖 IList 函数中,您可以使用反射来仅使用父属性填充列表.

                In the override IList function, you could use reflection to populate the list with only the parent properties perhaps.

                合同解析器应用于您的 json.net 序列化程序.这个例子来自一个 asp.net mvc 应用程序.

                Contract resolver is applied to your json.net serializer. This example is from an asp.net mvc app.

                JsonNetResult result = new JsonNetResult();
                result.Formatting = Formatting.Indented;
                result.SerializerSettings.ContractResolver = 
                    new CriteriaContractResolver<T>(Criteria);
                

                这篇关于使用 JSON.net,如何防止在基类上下文中使用派生类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 JsonConverter 仅序列化类的继承属性 下一篇:如何提高 .Net 中的 JSON 反序列化速度?(JSON.net 还是

                相关文章

                最新文章

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

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

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

                  2. <tfoot id='C1QPz'></tfoot>