Después de un poco de búsqueda, me encontré con esta pregunta:
How to inherit the attribute from interface to object when serializing it using JSON.NET
Tomé el código de Jeff esternal y añadió JsonIgnoreAttribute
detección, por lo que se parece a esto:
class InterfaceContractResolver : DefaultContractResolver
{
public InterfaceContractResolver() : this(false) { }
public InterfaceContractResolver(bool shareCache) : base(shareCache) { }
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
var interfaces = member.DeclaringType.GetInterfaces();
foreach (var @interface in interfaces)
{
foreach (var interfaceProperty in @interface.GetProperties())
{
// This is weak: among other things, an implementation
// may be deliberately hiding an interface member
if (interfaceProperty.Name == member.Name && interfaceProperty.MemberType == member.MemberType)
{
if (interfaceProperty.GetCustomAttributes(typeof(JsonIgnoreAttribute), true).Any())
{
property.Ignored = true;
return property;
}
if (interfaceProperty.GetCustomAttributes(typeof(JsonPropertyAttribute), true).Any())
{
property.Ignored = false;
return property;
}
}
}
}
return property;
}
}
Uso esto InterfaceContractResolver
en mi JsonSerializerSettings
, todas las propiedades que tienen un JsonIgnoreAttribute
en cualquier interfaz se ignoran, también, incluso si tienen un JsonPropertyAttribute
(debido al orden de la bloques internos if
).
Debe agregar esto como respuesta y aceptarlo, si resuelve su problema. – Groo
Lo hice ahora, pero no pude hacerlo mucho antes porque no podía hacerlo menos de 8 horas después de mi pregunta (y porque no estaba trabajando el fin de semana). – fero