Skip to content

Fix CheckSpecified setter for field-backed members in ReflectionXmlSerializationReader#126035

Open
haltandcatchwater wants to merge 2 commits intodotnet:mainfrom
haltandcatchwater:fix-reader-checkspecified-reflection
Open

Fix CheckSpecified setter for field-backed members in ReflectionXmlSerializationReader#126035
haltandcatchwater wants to merge 2 commits intodotnet:mainfrom
haltandcatchwater:fix-reader-checkspecified-reflection

Conversation

@haltandcatchwater
Copy link
Contributor

Summary

ReflectionXmlSerializationReader used GetMethod("set_X") to set XxxSpecified members during deserialization. This only finds property setters — for field-backed Specified members, GetMethod returns null and the setter is silently skipped, leaving the Specified flag as false even 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 via ILGenSet. This was the only call site using the raw GetMethod("set_X") pattern.

Related to #125859 (same class of reflection-path drift from the ILGen path).

Changes

  • ReflectionXmlSerializationReader.cs: Replace GetMethod("set_X") + ?.Invoke with GetSetMemberValueDelegate, resolving once and caching
  • SerializationTypes.cs: Add TypeWithFieldBackedSpecifiedMember with a public bool FooSpecified field
  • XmlSerializerTests.cs: Add test verifying FooSpecified is set to true after round-trip deserialization

…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.
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Serialization community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant