Replies: 1 comment
-
I ended up writing the following rudimentary code to get all the serializable types in a string that I can copy/paste above my custom serializer context: var typeFullNames = Assembly
.GetAssembly(typeof(SomeClassInMyLibrary))
.GetTypes()
.Where(t => t.IsClass)
.Where(t => t.Namespace?.StartsWith("MyLibrary.Models") ?? false)
.Select(t => $"{t.Namespace}.{t.Name}")
.OrderBy(t => t);
var result = string.Join("\r\n", typeFullNames.Select(name => $"[JsonSerializable(typeof({name}))]")); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I just started looking at new
System.text.Json
source generator and I understand that I must decorate my customJsonSerializerContext
with each and every class that is serializable/de-serializable like so:This is simple enough when you serialize a reasonably small number of classes but what about cases where you have a large number of classes? For example, my library contains roughly 200 model classes. I'm afraid that adding all 200
JsonSerializable
attributes to my custom serializer context will take forever and the likelihood of making a mistake and/or forgetting a few classes is very high.Is there a way the source generator can automatically discover all my model classes? The ideal solution for my scenario would be to be able to write something like this:
Alternatively, is there a tool I can use that will generate
[JsonSerializable(typeof(...))]
for all 200 classes which I could copy/paste to my custom serializer context.cs
file?Beta Was this translation helpful? Give feedback.
All reactions