我使用JsonSerializer ContractResolver来忽略空列表。我想再加一个忽略的条件具有特定名称
的属性var serilaizeJson = JsonConvert.SerializeObject(result, Formatting.None,
new JsonSerializerSettings
{
ContractResolver = SerializeContractResolver.Instance
});
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (property.PropertyType != typeof(string))
{
if (property.PropertyType.GetInterface(nameof(IEnumerable)) != null)
property.ShouldSerialize =
instance => (instance?.GetType().GetProperty(property.PropertyName).GetValue(instance) as IEnumerable<object>)?.Count() > 0 && .;
}
return property;
}
是否有任何方法将检查与当前检查合并,如果属性名称为CreatedOn忽略它。它是一种字符串值。
你可以这样写:
bool yourAttributeIsOnTheProperty = member.GetCustomAttributes(typeof(CreatedOn), false).Any();