Use case: I want to write a JsonConverter for a generic type like this, but I cannot apply it to the type itself:
public class EditingModelConverter<T> : JsonConverter<EditingModel<T>>
{
// ...
}
[JsonConverter(typeof(EditingModelConverter<T>))] // Error
public class EditingModel<T>(FrozenDictionary<string, object> values)
{
// ...
}
The error message is a CS0416:
'EditingModelConverter': an attribute argument cannot use type parameters
My questions is it possible to do this without writing a converter factory? I think a factory would be the most proper solution if there is no workaround?
System.Text.Jsontag.where T : structto be able to use the parameter as a type parameter to a attribute. blog.stephencleary.com/2022/10/…System.Attributeis not allowed. The attribute in this question isJsonConverterAttribute, which is not generic. The error is simply because the type parameterTis out of scope at that position. That said, I believe the question should stay closed until it is edited to ask only one question.