Skip to content

Fix bug with XmlSerializer and generic type handling wrt collectible ALCs#126784

Draft
StephenMolloy wants to merge 1 commit intodotnet:mainfrom
StephenMolloy:wt-100518-XmlSerializer_and_Collectible_ALC
Draft

Fix bug with XmlSerializer and generic type handling wrt collectible ALCs#126784
StephenMolloy wants to merge 1 commit intodotnet:mainfrom
StephenMolloy:wt-100518-XmlSerializer_and_Collectible_ALC

Conversation

@StephenMolloy
Copy link
Copy Markdown
Member

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:

  • Introduced the LoadContextAwareTypeResolver utility to consistently determine the correct assembly and load context for a given Type or array of types, handling generic and array types, and preferring non-default ALCs when present.
  • Updated usages throughout the serialization codebase to use LoadContextAwareTypeResolver instead of directly accessing Type.Assembly or AssemblyLoadContext.GetLoadContext, ensuring correctness in complex ALC scenarios. [1] [2] [3] [4] [5] [6]

Serializer Caching and Mapping Improvements:

  • Enhanced methods that construct or cache serializers to use all relevant types from mappings, not just a single root type, reducing cache misses and ensuring correct serializer lookup in multi-type scenarios. [1] [2] [3]
  • Added a helper method GetTypesFromMappings to extract all unique types from mappings and the root type for consistent use in serializer construction and caching.

Test Coverage Expansion:

  • Extended the existing test for types in collectible ALCs (Xml_TypeInCollectibleALC) to run with both standalone and collection root types, verifying correct serialization/deserialization and cache behavior for these scenarios. [1] [2]
  • Refactored the test harness to support collection roots, and added assertions to ensure correct serializer behavior and object equality for both single objects and collections. [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

…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.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 LoadContextAwareTypeResolver to consistently pick the “effective” load context/assembly for a Type, 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 XmlSerializer creation 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XmlSerializer does not work with collections in collectible ALC

2 participants