© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•10mo ago•
1 reply
Mr. Dink

STJ Polymorphic Serialization in Source Gen mode

I trying to remain AOT compatible for my library. I have a class that users can inherit from, so I'm unable to add the derived type attributes to the base type directly. I'm able to have the user register their custom types all in one place using a static method, and internally use a Modifier to add them to the list of derived types for that
JsonTypeInfo
JsonTypeInfo
. This works for reflection-based
DefaultJsonTypeInfoResolver
DefaultJsonTypeInfoResolver
, but no matter what I do, the delegate passed into
WithAddedModifier()
WithAddedModifier()
is never called if I use the source gen context and it just errors. Is there a way to add these in source gen mode?

Here's a gist: https://gist.github.com/markdibarry/354e2b9ed387e9b2f94088d2ad00c496
Gist
Polymorphic serialization with Source Gen
Polymorphic serialization with Source Gen. GitHub Gist: instantly share code, notes, and snippets.
Polymorphic serialization with Source Gen
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ [Challenge / Help] Sanitizing C# before serialization with STJ:
C#CC# / help
9mo ago
❔ What is the purpose of `ModelBindingContext.Model`, `ModelName`, and `ModelBinderAttribute.Name`?
C#CC# / help
3y ago
✅ Source-gen on a .shproj
C#CC# / help
4y ago
❔ Avoid infinite recursion in STJ converters
C#CC# / help
3y ago