Skip to content

Fix GetFieldByName to return derived class field when names are shadowed#1371

Merged
leculver merged 1 commit intomicrosoft:mainfrom
leculver:issue_1370
Feb 18, 2026
Merged

Fix GetFieldByName to return derived class field when names are shadowed#1371
leculver merged 1 commit intomicrosoft:mainfrom
leculver:issue_1370

Conversation

@leculver
Copy link
Copy Markdown
Contributor

When a derived class declares a field with the same name as a base class field, GetFieldByName was returning the base class field because Fields is ordered base-first and FirstOrDefault picks the first match.

Changed to LastOrDefault so the most-derived field is returned, matching C# shadowing semantics. Fixes #1370.

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 pull request fixes a bug in GetFieldByName where it would incorrectly return a base class field instead of a derived class field when both classes declare fields with the same name. The fix changes from FirstOrDefault to LastOrDefault to match C# field shadowing semantics, where the most-derived field should be returned.

Changes:

  • Modified ClrDacType.GetFieldByName to use LastOrDefault instead of FirstOrDefault, ensuring the most-derived field is returned when field names are shadowed in inheritance hierarchies

Comment thread src/Microsoft.Diagnostics.Runtime/Implementation/ClrDacType.cs
Comment thread src/Microsoft.Diagnostics.Runtime/Implementation/ClrDacType.cs
Changed FirstOrDefault to LastOrDefault in both ClrDacType and
ClrStringType so that the most-derived field is returned when base
and derived classes have fields with the same name.

Fixes microsoft#1370.
@leculver leculver merged commit f4a124c into microsoft:main Feb 18, 2026
8 checks passed
@leculver leculver deleted the issue_1370 branch February 18, 2026 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fileds duplication case

3 participants