Attributes don't do anything of themselves, they just get compiled into your types as metadata. Attributes generally don't "run" or "do something".
JSON.NET looks for its own JsonIgnore
attribute (and others) on type members using reflection.
So while you're asking for "best practices" and "better way" about inheriting JsonIgnoreAttribute
, but there simply is no way to do what you want, other than modify JSON.NET, issue a pull request and hope it gets accepted (it likely won't).
There are other ways to affect serialization though. You may research that. Why would you want to do this anyway, what's wrong with [JsonIgnore]
?
The question you link to, does sketch just that: add a contract resolver (a built-in extension point for affecting serialization in JSON.NET) which inspects your types for your custom attributes. That should work. And then for different serialization scenarios, you pass different contract resolvers. If you need help getting that to work, then ask about that, and not about inheriting sealed attributes.
And as @MarcGravell indicates, if you have different output types, use different DTO classes and something like AutoMapper to populate your DTOs. And perhaps try and use inheritance where relevant. Letting one type serialize differently based on some condition will bite you in the back sooner or later. Have you tested deserialization, for example?