Skip to content

Fix FieldInfo.GetValue for enum literals on open generic types on NativeAOT and add test - #138474

Open
Manish-1kumar wants to merge 4 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/fieldinfo-open-generic-enum-test
Open

Manish-1kumar wants to merge 4 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/fieldinfo-open-generic-enum-test

Conversation

@Manish-1kumar

@Manish-1kumar Manish-1kumar commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7976

FieldInfo.GetValue on a literal field of an enum nested in an open generic type (for example typeof(Generic<>.GenericEnum)) must not produce an instance of an open generic type. On CoreCLR it returns the underlying value since #129424. On NativeAOT it threw PlatformNotSupportedException ("TypeHandles are not supported for types that return true for ContainsGenericParameters").

NativeAOT fix: the literal field accessor was created with FieldType.TypeHandle, which throws for types with ContainsGenericParameters. LiteralFieldAccessor never uses the handle (it returns the stored constant and its setters throw), so CreateLiteralFieldAccessor no longer takes one. The constant itself is already returned as the underlying value by DefaultValueParser (#129424).

Test: adds FieldInfoTests.GetValue_EnumLiteralFieldOnOpenGenericType_ReturnsUnderlyingValue. #129424 added coverage for ParameterInfo.DefaultValue and the enum APIs, but nothing exercised FieldInfo.GetValue on the enum's literal field. The test uses a byte-backed enum, so it checks that the value comes back as the underlying type rather than an int by coincidence.

Verification:

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 29, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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.

🟢 Approval recommended

No active review issues were identified for the test-only change adding regression coverage for enum literals in open generic types.

No open issues 🎉

What changed in this PR

Adds regression coverage for #7976 to the reflection test suite, verifying the existing behavior established by #129424 without changing runtime code.

Changes:

  • Tests that FieldInfo.GetValue on an enum literal nested in an open generic type returns the underlying value.
  • Adds a byte-backed enum fixture and asserts both the exact returned type and value, preventing an int result from passing accidentally.
File Description
src/​libraries/​System.Runtime/​tests/​System.Reflection.Tests/​FieldInfoTests.cs Adds the open-generic enum literal regression test and its byte-backed enum fixture.

Review effort: Balanced

@jkotas

jkotas commented Nov 29, 2026

Copy link
Copy Markdown
Member

The test fails with NAOT. Can NAOT be fixed to make the test pass?

NativeAOT throws PlatformNotSupportedException for field access on types with
ContainsGenericParameters.
…tiveAOT

The literal field accessor was created with the field type's RuntimeTypeHandle,
which throws PlatformNotSupportedException for types with
ContainsGenericParameters. The handle isn't used for literal fields, so stop
passing it, and run the regression test on NativeAOT again.
@Manish-1kumar

Copy link
Copy Markdown
Contributor Author

Fixed NativeAOT in 4f67519 instead of disabling the test. The literal field accessor was created with FieldType.TypeHandle, which throws PlatformNotSupportedException for types with ContainsGenericParameters, but LiteralFieldAccessor never uses the handle (it returns the stored constant and its setters throw). So CreateLiteralFieldAccessor no longer takes a type handle. The constant itself was already returned as the underlying value by DefaultValueParser after #129424.

The test is a plain [Fact] again. Locally, System.Reflection.Tests passes both on CoreCLR (2123/2123) and compiled with NativeAOT (/p:TestNativeAot=true: 2071 run, 0 failures, 47 skipped).

@Manish-1kumar Manish-1kumar changed the title Add FieldInfo.GetValue test for enum literals on open generic types Fix FieldInfo.GetValue for enum literals on open generic types on NativeAOT and add test Nov 30, 2026
@Manish-1kumar

Copy link
Copy Markdown
Contributor Author

CI doesn't seem to have started for the last two pushes (4f67519, bf4c053). Could someone run /azp run runtime?

@MichalStrehovsky

Copy link
Copy Markdown
Member

CI doesn't seem to have started for the last two pushes (4f67519, bf4c053). Could someone run /azp run runtime?

There is a known outage right now.

@Manish-1kumar

Copy link
Copy Markdown
Contributor Author

Thanks! It looks like the trigger for bf4c053 was lost during the outage. Could you run /azp run runtime when you get a chance?

@MichalStrehovsky

Copy link
Copy Markdown
Member

Thanks! It looks like the trigger for bf4c053 was lost during the outage. Could you run /azp run runtime when you get a chance?

I haven't forgotten about this, but the CI is still offline.

This branch has not been deployed

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

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.

Reflection on enums creates instances of open generic types

4 participants