using System.Reflection; using InterfaceForward.Domain.Shared.Dtos; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; namespace InterfaceForward.Application.Services.InterfaceMap; internal class CustomContractResolver : DefaultContractResolver { protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { JsonProperty property = base.CreateProperty(member, memberSerialization); if (ShouldIgnoreProperty(member)) { property.ShouldSerialize = _ => false; // 设置不序列化的条件 } return property; } protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) { IList properties = base.CreateProperties(type, memberSerialization); var children = properties.FirstOrDefault(x => x.PropertyName == nameof(ParameterNode.Children)); if (children != null) { properties.Remove(children); properties.Add(children); } return properties; } private bool ShouldIgnoreProperty(MemberInfo member) { if (member.DeclaringType == typeof(InterfaceParameterMapDto)) { if (member.Name.IsIn(nameof(InterfaceParameterMapDto.Id), nameof(InterfaceParameterMapDto.PId), nameof(InterfaceParameterMapDto.InterfaceId), nameof(InterfaceParameterMapDto.FreeMap))) return true; } // 其他判断逻辑... return false; } }