Warning about Trimmers and XmlSerializers
My library uses a trimmer to handle complex XML. But when I turned on code trimming for it, it showed warning
IL2026: [...] Members from serialized types may be trimmed if not referenced directly.
, the code looked like this:
I looked everywhere but didn't find any direct solutions, and the .NET XML serialization code seems to have been out of date for a long time, and lacks many features to circumvent reflection mechanisms, such as Source Generation, compared to JSON.
I don't want to overcomplicate the code, so my own guess at a solution is to create a new class and use the DynamicalAccessedMembersAttribute to prevent the trimmer from trimming key class fields and properties:
Is this correct? Are there any other possible correct options I have?2 Replies
Not sure how fully-featured it is, but there's this package that uses source generators for XML: https://github.com/ZingBallyhoo/StackXML
The existing class definitions are generated by xsd.exe. It seems that using the new library will result in a relatively large change, and my library is still in .NET 8, but it is a way worth considering.:meow3c: Anyway thanks for your advice.:meowheart: