Reputation: 3183
I am trying to convert a particular IDictionary
field of a particular class differently. I was thinking of doing this based on an attribute, but it looks like it wont work.
Is it possible to somehow get the CustomAttribute
associated with the type in CanConvert
?
[CustomAttribute]
public IDictionary<string, string> AdditionalData { get; set; }
// Custom converter for CustomAttribute
public override bool CanConvert(Type objectType)
{
return System.Attribute.GetCustomAttributes(objectType).Any(v => v is CustomAttribute);
}
Upvotes: 1
Views: 1929
Reputation: 129667
If you want to use an attribute to apply a converter to a specific property, just use a [JsonConverter]
attribute. There's really no need to invent your own.
[JsonConverter(typeof(YourCustomConverterClass))]
public IDictionary<string, string> AdditionalData { get; set; }
Note that if you apply a [JsonConverter]
attribute it is no longer necessary to pass an instance of that converter to the serializer. Also, the converter's CanConvert
method will not be called for that property because Json.Net already knows you want to use the converter with it.
Upvotes: 3