Pretty straightforward question. I want to allow only DTO models in inputs (class & record types).
If I leave the class
constraint only, I'm still able to put string
in there. I want to not allow the primitive types.
If I put class, new()
, it doesn't allow string
just as expected, but I'm not allowed to use positional records either which is a problem.
public class Asd
{
public string Name { get; set; }
}
public record Asd2(string Name);
public Task DoSomethingAsync<TInput>(TInput data) where TInput : class
{
var message = JsonSerializer.Serialize(data); // if TInput is a string, it results into faulty \u0022 codes
}
Edit: The actual issue is that I have JsonSerializer.Serialize(data)
inside that method and if a string
is passed accidentally, it will result in something like:
"{\u0022Name\u0022:\u0022Test\u0022}"