Skip to content

Share Attribute implementation with NativeAOT - #132821

Open
MichalStrehovsky wants to merge 4 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-share-attribute-implementation
Open

Share Attribute implementation with NativeAOT#132821
MichalStrehovsky wants to merge 4 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-share-attribute-implementation

Conversation

@MichalStrehovsky

@MichalStrehovskyMichalStrehovsky commented Aug 27, 2026

Copy link
Copy Markdown
Member

Deletes 600+ lines of native AOT specific code.

Fixes#73356

Use the CoreCLR Attribute implementation in NativeAOT and align the runtime reflection custom attribute layering. Remove the duplicated NativeAOT facade, legacy API aggregation, and generic searcher hierarchy.

Use the CoreCLR Attribute implementation in NativeAOT and align the runtime reflection custom attribute layering. Remove the duplicated NativeAOT facade, legacy API aggregation, and generic searcher hierarchy.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cdfa51d4-b08c-4fc4-836d-ad41d04d3c15
@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.

@MichalStrehovsky

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 160e0b66-0384-4c50-b528-c9cd36496cee
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 69dacc0c-3e1b-4730-b772-f4e6132650a4
@MichalStrehovsky
MichalStrehovsky marked this pull request as ready for review August 28, 2026 05:16
CopilotAI lite review requested due to automatic review settings August 28, 2026 05:16
@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.

CopilotAI 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.

Pull request overview

This PR refactors NativeAOT’s reflection custom-attribute stack to reuse the CoreCLR Attribute implementation and a shared matching/instantiation helper, removing duplicated NativeAOT-only attribute searcher/facade layers. It also re-enables the previously skipped System.Reflection.Context tests on NativeAOT by addressing the underlying attribute-layering mismatch.

Changes:

  • Switch NativeAOT to compile and use System/Attribute.CoreCLR.cs, exposing Attribute.InternalGetAttributeUsage for the shared attribute matching logic.
  • Introduce RuntimeCustomAttribute for NativeAOT and route GetCustomAttributes*/IsDefined/GetCustomAttributesData overrides through it across reflection runtime types.
  • Delete legacy NativeAOT custom attribute aggregation/searcher infrastructure and re-enable previously skipped tests tied to #73356.

Reviewed changes

Copilot reviewed 28 out of 28 changed files in this pull request and generated no comments.

Show a summary per file
FileDescription
src/libraries/System.Reflection.Context/tests/CustomReflectionContextTests.csRemoves NativeAOT ActiveIssue gate so the fixed scenario runs on NativeAOT.
src/libraries/System.Reflection.Context/tests/CustomReflectionContext.Examples.csRemoves NativeAOT ActiveIssue gate for the example-based test.
src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.csMakes InternalGetAttributeUsage accessible for reuse and adds helper for attribute array creation; adds NativeAOT-specific usings.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttribute.NativeAot.csNew shared helper for matching/instantiating attributes (including inheritance walk where applicable).
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.csRoutes custom-attribute APIs through RuntimeCustomAttribute.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.csRoutes custom-attribute APIs through RuntimeCustomAttribute (desktop-compat “inherit ignored” behavior preserved).
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/ParameterInfos/RuntimeParameterInfo.csRoutes custom-attribute APIs through RuntimeCustomAttribute (desktop-compat “inherit ignored” behavior preserved).
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/Modules/RuntimeModule.csRoutes custom-attribute APIs through RuntimeCustomAttribute (inherit meaningless).
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodInfo.csRoutes custom-attribute APIs through RuntimeCustomAttribute; updates base-definition traversal to the new override helper.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimeConstructorInfo.csRoutes custom-attribute APIs through RuntimeCustomAttribute.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/LegacyCustomAttributeApis.csDeletes legacy NativeAOT per-type “older-style” custom-attribute API implementations.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/Helpers.csRemoves the old InstantiateAsArray helper now superseded by the new attribute pipeline.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.csRoutes custom-attribute APIs through RuntimeCustomAttribute.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/EventInfos/RuntimeEventInfo.csRoutes custom-attribute APIs through RuntimeCustomAttribute (desktop-compat “inherit ignored” behavior preserved).
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/Shared.csReworks implicit-override discovery into a MethodInfo-focused helper with suppression for reflection analysis.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/PropertyPolicies.csRemoves now-unneeded policy members tied to the old generic override/member enumeration pipeline.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/NestedTypePolicies.csRemoves now-unneeded policy members tied to the old generic override/member enumeration pipeline.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MethodPolicies.csAdjusts override detection entrypoint to a static helper for the new override discovery flow.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/MemberPolicies.csRemoves abstract members that were only required for the legacy generic override/member enumeration path.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/FieldPolicies.csRemoves now-unneeded policy members tied to the old generic override/member enumeration pipeline.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/EventPolicies.csRemoves now-unneeded policy members tied to the old generic override/member enumeration pipeline.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/BindingFlagSupport/ConstructorPolicies.csRemoves now-unneeded policy members tied to the old generic override/member enumeration pipeline.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/Assemblies/RuntimeAssemblyInfo.csRoutes custom-attribute APIs through RuntimeCustomAttribute (inherit meaningless).
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Attribute.NativeAot.csDeletes the NativeAOT-specific Attribute implementation in favor of the shared CoreCLR one.
src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csprojIncludes CoreCLR Attribute.CoreCLR.cs, adds RuntimeCustomAttribute.NativeAot.cs, and removes legacy custom-attribute sources.
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Extensions/NonPortable/CustomAttributeSearcher.csDeletes the legacy generic searcher hierarchy.
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Extensions/NonPortable/CustomAttributeInheritanceRules.csDeletes legacy matching/inheritance extension layer superseded by the shared implementation.
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Augments/ReflectionAugments.csRemoves wrappers that depended on the old generic override/member discovery APIs.

@MichalStrehovsky

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@MichalStrehovsky

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).


#if NATIVEAOT
using System.Reflection.Runtime.MethodInfos;
using Internal.Reflection.Augments;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

using Internal.Reflection.Augments; should not be needed.

We can get rid of using System.Reflection.Runtime.MethodInfos; by changing RuntimeMethodInfo namespace to System.Reflection.

</ItemGroup>
<ItemGroup>
<!-- TODO: (async) once we know which helpers can actually be shared, move those to libraries partition -->
<CompileInclude="$(CoreClrProjectRoot)System.Private.CoreLib\src\System\Attribute.CoreCLR.cs" />

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I assume that this is here just to make the diff easier to review, and it will be moved to libraries Attribute.cs in a follow up

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NativeAot] System.Reflection.Context.Tests.CustomReflectionContextTests.MapType_MemberAttributes_Success

3 participants

@MichalStrehovsky@jkotas