Repository navigation
Implement HasSameMetadataDefinitionAs in System.Reflection.Context - #135244
Merged
Merged
Conversation
Types and members returned by a `CustomReflectionContext` throw `NotImplementedException` from `HasSameMetadataDefinitionAs`, the `MemberInfo` base, because no class in the library overrides it. `Type.GetMemberWithSameMetadataDefinitionAs` calls it on every member of the type, so it throws too. The projecting type and member classes now compare the members they wrap, and match only members projected by the same context, as their `Equals` does; the runtime's own implementations likewise match only their own member classes. Properties the context adds, and their accessors, have no metadata definition: each matches only itself, including the copy a derived type reports with a different `ReflectedType`. Fixes dotnet#135242 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection |
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The implementation is consistent across member kinds and includes focused regression coverage for the affected behavior.
Review effort: Balanced
Findings: None
What changed in this PR
Implements metadata-definition comparison for members projected by CustomReflectionContext, enabling GetMemberWithSameMetadataDefinitionAs.
Changes:
- Compares projected members through their underlying metadata and reflection context.
- Defines identity semantics for added properties and inherited accessors.
- Adds comprehensive regression coverage.
| File | Description |
|---|---|
System.Reflection.Context.Tests.csproj |
Includes the new tests. |
HasSameMetadataDefinitionAsTests.cs |
Covers mapped, generic, added, inherited, and null cases. |
VirtualPropertyBase.cs |
Compares added properties. |
VirtualMethodBase.cs |
Compares added accessors. |
InheritedPropertyInfo.cs |
Delegates property comparison. |
InheritedMethodInfo.cs |
Delegates accessor comparison. |
ProjectingType.cs |
Compares projected types. |
ProjectingPropertyInfo.cs |
Compares projected properties. |
ProjectingMethodInfo.cs |
Compares projected methods. |
ProjectingFieldInfo.cs |
Compares projected fields. |
ProjectingEventInfo.cs |
Compares projected events. |
ProjectingConstructorInfo.cs |
Compares projected constructors. |
jkotas
approved these changes
Oct 6, 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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Types and members returned by a
CustomReflectionContextthrowNotImplementedExceptionfromHasSameMetadataDefinitionAs, because no class in the library overrides theMemberInfobase.Type.GetMemberWithSameMetadataDefinitionAscalls it on every member of the type, so it throws too.Changes
Equalsdoes. Like the runtime's own members, a mapped member never matches a runtime member.ReflectedType, since the runtime leavesReflectedTypeout of the comparison.GetMemberWithSameMetadataDefinitionAsneeds no override:Type's implementation now works, added properties included.ArgumentNullException, as for the runtime's members.Other behaviours for added members, not chosen:
false: not reflexive, andGetMemberWithSameMetadataDefinitionAs(addedProperty)throwsArgumentExceptionfor a property the type reports.MemberInfoTestsrecords that case "for future compat" and calls it surprising, andGetMemberWithSameMetadataDefinitionAswould return the first added member of that kind.GetMembers, so with only the mapped members fixed, looking up a mapped field on a type with an added property still throwsNotImplementedException.Tests
New
HasSameMetadataDefinitionAsTestscover mapped types (including generic definitions and nested types), every kind of mapped member, added properties and their accessors on base and derived types,GetMemberWithSameMetadataDefinitionAs, and null arguments. Every new test fails without the change; with it, they pass, as do the rest ofSystem.Reflection.Context.TestsandSystem.ComponentModel.Composition.Registration.Tests.Not in this PR
On a derived mapped type,
GetMethods()returns the base type's accessors of an added property, soGetMemberWithSameMetadataDefinitionAsreturns the base type's getter there (both match). That is #135240, fixed in #135241.Resolves #135242
Note
AI-generated, written at my direction and reviewed by me before posting. The tests ran on a local Windows x64 build of
main(CoreCLR Checked, Debug libraries). With this change merged locally with the related changes, the new tests also passed on local builds for Mono (JIT and interpreter), NativeAOT and browser-wasm (V8). The behaviour under "Not in this PR" was measured on .NET 10.0.12 with the 10.0.12 package.