Add PreserveAttribute to generic formatters for Unity IL2CPP #2136
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In Unity's AOT environment, MakeGenericType often works for Full Generic Sharing.
https://unity.com/blog/engine-platform/il2cpp-full-generic-sharing-in-unity-2022-1-beta
Therefore, even with AvoidDynamicCode, we include DynamicGenericResolver in StandardResolver.
However, if the Formatter itself is stripped, it naturally won't function.
While this behavior changes depending on Unity's build settings' StrippingLevel,
we want MessagePack for C# to operate as naturally as possible even at the highest stripping level.
To achieve this, we'll make Generic Formatters preserve.
Since PreserveAttribute can be application-specific, we'll implement it as an internal type.
https://docs.unity3d.com/6000.0/Documentation/Manual/managed-code-stripping-preserving.html
Additionally, when supporting the generation of Generic-compatible Formatters,
it might be worth considering making it a public type and applying it to generated types.
#2134 is also related, Having looked into Native AOT related matters recently, I'm starting to think it's better to settle for an approach where things work - even if they might result in runtime errors in some cases - with workarounds available, rather than obsessing over 100% compatibility.