Skip to content

Implement HasSameMetadataDefinitionAs in System.Reflection.Context - #135244

Merged
jkotas merged 1 commit into
dotnet:mainfrom
caraioniurie47:issue-135242
Oct 6, 2026
Merged

jkotas merged 1 commit into
dotnet:mainfrom
caraioniurie47:issue-135242

Conversation

@caraioniurie47

Copy link
Copy Markdown
Contributor

Types and members returned by a CustomReflectionContext throw NotImplementedException from HasSameMetadataDefinitionAs, because no class in the library overrides the MemberInfo base. Type.GetMemberWithSameMetadataDefinitionAs calls it on every member of the type, so it throws too.

Changes

  • The projecting type and member classes compare the members they wrap, and match only members projected by the same context, as their Equals does. Like the runtime's own members, a mapped member never matches a runtime member.
  • A property the context adds, and each of its accessors, has no metadata definition. Each matches only itself, including the copy a derived mapped type reports with a different ReflectedType, since the runtime leaves ReflectedType out of the comparison.
  • GetMemberWithSameMetadataDefinitionAs needs no override: Type's implementation now works, added properties included.
  • A null argument throws ArgumentNullException, as for the runtime's members.

Other behaviours for added members, not chosen:

  • Always false: not reflexive, and GetMemberWithSameMetadataDefinitionAs(addedProperty) throws ArgumentException for a property the type reports.
  • All added members match each other, like CoreCLR's array methods that share one token: MemberInfoTests records that case "for future compat" and calls it surprising, and GetMemberWithSameMetadataDefinitionAs would return the first added member of that kind.
  • Keep throwing for added members: they are in GetMembers, so with only the mapped members fixed, looking up a mapped field on a type with an added property still throws NotImplementedException.

Tests

New HasSameMetadataDefinitionAsTests cover 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 of System.Reflection.Context.Tests and System.ComponentModel.Composition.Registration.Tests.

Not in this PR

On a derived mapped type, GetMethods() returns the base type's accessors of an added property, so GetMemberWithSameMetadataDefinitionAs returns 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.

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]>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 5, 2026
@azure-pipelines

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
jkotas merged commit f05ec50 into dotnet:main Oct 6, 2026
94 of 95 checks passed
@caraioniurie47
caraioniurie47 deleted the issue-135242 branch October 6, 2026 07:06
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 8, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 8, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Reflection 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.

HasSameMetadataDefinitionAs throws NotImplementedException on types and members from CustomReflectionContext

3 participants