如
public class Person { public string Name{get;set;} public int Age{get;set;} } public class Student : Person { public string Grade {get;set;} [Newtonsoft.Json.JsonIgnore] public new int Age{get;set;} }
序列化Student,Age还是一样的输出
写一个转换类
using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.Collections.Generic; using System.Linq; using System.Reflection;
/// <summary> /// 继承父类后,设置忽略序列化修补设置 /// </summary> public class JsonIgnoreContractResolver : DefaultContractResolver { //protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) //{ // JsonProperty property = base.CreateProperty(member, memberSerialization); // //var ignore = property.GetCustomAttributes(typeof(JsonIgnoreAttribute), inherit: false) // // .OfType<JsonIgnoreAttribute>().SingleOrDefault(); // if (member.IsDefined(typeof(JsonIgnoreAttribute))) // { // property.ShouldSerialize = instance => false; // } // return property; //} protected override List<MemberInfo> GetSerializableMembers(Type objectType) { //Return properties that do NOT have the JsonIgnoreSerializationAttribute return objectType.GetProperties() .Where(pi => !Attribute.IsDefined(pi, typeof(JsonIgnoreAttribute))) .ToList<MemberInfo>(); } }
使用
var settings = new JsonSerializerSettings(); settings.ContractResolver = new JsonIgnoreContractResolver();
指定转换设置,进行操作,就能忽略掉标记了
[Newtonsoft.Json.JsonIgnore]
的属性
标签:set,JsonIgnore,子类,System,Json,Api,using,public From: https://www.cnblogs.com/DoNetCShap/p/18380613