Fix bug with XmlSerializer and generic type handling wrt collectible ALCs#126784
Draft
StephenMolloy wants to merge 1 commit intodotnet:mainfrom
Draft
Fix bug with XmlSerializer and generic type handling wrt collectible ALCs#126784StephenMolloy wants to merge 1 commit intodotnet:mainfrom
StephenMolloy wants to merge 1 commit intodotnet:mainfrom
Conversation
…handling - Introduced LoadContextAwareTypeResolver to centralize load context logic. - Updated methods in Compilation, ContextAwareTables, and XmlSerializer to utilize the new resolver. - Enhanced tests to validate behavior with collectible ALCs and added inline data for better coverage.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves XmlSerializer behavior for types loaded from custom (especially collectible) AssemblyLoadContexts, including scenarios where the root type is a CoreLib “wrapper” generic (e.g., List<T>) but the effective load context is determined by a collectible generic argument.
Changes:
- Introduces
LoadContextAwareTypeResolverto consistently pick the “effective” load context/assembly for aType, including generic/array element traversal. - Updates serializer generation/caching paths to consider all relevant types from mappings (not just a single root type), improving correctness for multi-type mappings in collectible ALC scenarios.
- Expands the collectible ALC test to cover both single-object and collection-root cases, and validates behavior through
XmlSerializercreation APIs (new XmlSerializer,FromTypes,FromMappings).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs | Extends the collectible ALC regression test to cover collection roots and multiple serializer construction paths. |
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs | Uses mapping-derived type sets for temp assembly creation/caching; adjusts FromTypes to pass a root type when appropriate. |
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/ContextAwareTables.cs | Adds LoadContextAwareTypeResolver and uses it to select collectible vs default tables based on effective load context. |
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compilation.cs | Uses load-context-aware assembly selection for contextual reflection, ref-emit main assembly selection, verification, and cache keying. |
This was referenced Apr 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request refactors the XML serialization code to improve handling of types loaded in custom AssemblyLoadContexts (ALCs), especially for scenarios involving collectible ALCs and generic types. The changes centralize and enhance logic for determining the appropriate assembly and load context for types, and expand test coverage to ensure correct serializer behavior with types in custom and collectible ALCs.
Key improvements include:
AssemblyLoadContext and Type Resolution Enhancements:
LoadContextAwareTypeResolverutility to consistently determine the correct assembly and load context for a givenTypeor array of types, handling generic and array types, and preferring non-default ALCs when present.LoadContextAwareTypeResolverinstead of directly accessingType.AssemblyorAssemblyLoadContext.GetLoadContext, ensuring correctness in complex ALC scenarios. [1] [2] [3] [4] [5] [6]Serializer Caching and Mapping Improvements:
GetTypesFromMappingsto extract all unique types from mappings and the root type for consistent use in serializer construction and caching.Test Coverage Expansion:
Xml_TypeInCollectibleALC) to run with both standalone and collection root types, verifying correct serialization/deserialization and cache behavior for these scenarios. [1] [2]These changes make the XML serialization infrastructure more robust and reliable in advanced loading scenarios, such as dynamic plugin systems or environments using collectible assemblies.
Fixes: #100518