Fix CheckSpecified setter for field-backed members in ReflectionXmlSerializationReader#126035
Merged
StephenMolloy merged 4 commits intodotnet:mainfrom Apr 25, 2026
Conversation
…herited ShouldSerialize methods ReflectionXmlSerializationWriter used BindingFlags.DeclaredOnly when looking up ShouldSerialize methods at two call sites, which prevented finding methods declared on base classes. This caused a NullReferenceException when serializing derived types whose base class defines the ShouldSerialize method. The MemberMapping already stores the correctly resolved MethodInfo (CheckShouldPersistMethodInfo), which is resolved without DeclaredOnly during import and used by the IL-gen path. Use it directly in the reflection path to match the IL-gen behavior. Fix dotnet#120561
…ld-backed members
ReflectionXmlSerializationReader used GetMethod("set_X") to set
XxxSpecified members during deserialization. This only finds property
setters, silently skipping field-backed Specified members. The rest of
the reflection reader uses GetSetMemberValueDelegate which handles
both fields and properties and caches the delegate.
Replace the GetMethod call with GetSetMemberValueDelegate to align
with the existing pattern and fix deserialization of types with
field-backed Specified members.
This was referenced Mar 25, 2026
Open
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes reflection-based XmlSerializer deserialization so XxxSpecified members are correctly set for both property- and field-backed patterns, aligning the reflection path with existing cached delegate infrastructure and the ILGen path.
Changes:
- Update
ReflectionXmlSerializationReaderto set*SpecifiedviaGetSetMemberValueDelegate(supports fields + caching). - Add a new serialization test type with a field-backed
FooSpecifiedmember. - Add/extend XmlSerializer tests to cover field-backed
*Specifiedand inheritedShouldSerialize*behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs | Uses cached member-setter delegates to set XxxSpecified reliably for fields and properties during deserialization. |
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs | Uses pre-resolved CheckShouldPersistMethodInfo instead of doing a GetMethod(...) lookup at write time. |
| src/libraries/System.Runtime.Serialization.Xml/tests/SerializationTypes.cs | Adds test model types for inherited ShouldSerialize* and field-backed FooSpecified. |
| src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs | Adds tests validating inherited ShouldSerialize* behavior and that field-backed FooSpecified is set on deserialize. |
This was referenced Apr 25, 2026
StephenMolloy
approved these changes
Apr 25, 2026
Member
StephenMolloy
left a comment
There was a problem hiding this comment.
I added the updated "Expected" generated serializer code to include the new types in SerializationTypes.
Otherwise, lgtm
Member
|
/ba-g unrelated test failure |
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.
Summary
ReflectionXmlSerializationReaderThis pull request improves the XML serialization and deserialization logic by optimizing how "ShouldSerialize" and "Specified" members are handled, and adds new tests to ensure correct behavior for inherited serialization methods and field-backed "Specified" members.
Serialization and Deserialization Logic Improvements:
MethodInfoinstances (CheckShouldPersistMethodInfo) instead of repeated reflection lookups, improving performance and maintainability. [1] [2]GetSetMemberValueDelegate) rather than reflection on every call, making the process more efficient and robust.Test Coverage Enhancements:
Test Types Additions:
BaseTypeWithShouldSerializeMethod,DerivedTypeWithInheritedShouldSerialize, andTypeWithFieldBackedSpecifiedMemberto support the above tests.usedGetMethod("set_X")to setXxxSpecifiedmembers during deserialization. This only finds property setters — for field-backedSpecifiedmembers,GetMethodreturns null and the setter is silently skipped, leaving theSpecifiedflag asfalseeven when the element is present in the XML.The rest of the reflection reader already uses
GetSetMemberValueDelegate(which handles both fields and properties and caches the delegate). The ILGen reader handles both viaILGenSet. This was the only call site using the rawGetMethod("set_X")pattern.Related to #125859 (same class of reflection-path drift from the ILGen path).
Changes
GetMethod("set_X")+?.InvokewithGetSetMemberValueDelegate, resolving once and cachingTypeWithFieldBackedSpecifiedMemberwith apublic bool FooSpecifiedfieldFooSpecifiedis set totrueafter round-trip deserialization