Skip to content

Fix ConstructorInfo.GetGenericArguments to return empty array#128059

Merged
steveisok merged 6 commits into
dotnet:mainfrom
steveisok:fix-constructorinfo-getgenericarguments
May 13, 2026
Merged

Fix ConstructorInfo.GetGenericArguments to return empty array#128059
steveisok merged 6 commits into
dotnet:mainfrom
steveisok:fix-constructorinfo-getgenericarguments

Conversation

@steveisok
Copy link
Copy Markdown
Member

Constructors are never generic methods in the CLR (there is no IL or language support for generic constructors). Previously, calling GetGenericArguments() on a ConstructorInfo threw NotSupportedException because the call fell through to the base MethodBase implementation, which throws by design when not overridden.

RuntimeMethodInfo returns an empty array for non-generic methods, but RuntimeConstructorInfo did not override the method and so threw. Add a single override on the abstract ConstructorInfo base so the fix applies uniformly to all subclasses (CoreCLR, Mono, NativeAOT, MetadataLoadContext, custom ConstructorInfo subclasses, etc.).

Fixes #128041

Constructors are never generic methods in the CLR (there is no IL or
language support for generic constructors). Previously, calling
GetGenericArguments() on a ConstructorInfo threw NotSupportedException
because the call fell through to the base MethodBase implementation,
which throws by design when not overridden.

RuntimeMethodInfo returns an empty array for non-generic methods, but
RuntimeConstructorInfo did not override the method and so threw. Add a
single override on the abstract ConstructorInfo base so the fix applies
uniformly to all subclasses (CoreCLR, Mono, NativeAOT,
MetadataLoadContext, custom ConstructorInfo subclasses, etc.).

Fixes dotnet#128041

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 12, 2026 00:48
@steveisok steveisok requested a review from a team May 12, 2026 00:49
@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.

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 PR changes ConstructorInfo.GetGenericArguments() to return an empty Type[] instead of falling back to MethodBase.GetGenericArguments() (which throws). This makes constructors behave like other non-generic methods when queried for generic arguments.

Changes:

  • Override ConstructorInfo.GetGenericArguments() in System.Private.CoreLib to always return an empty array.
  • Add a regression test asserting ConstructorInfo.GetGenericArguments() is empty for constructors on both non-generic and generic types.

Reviewed changes

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

File Description
src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs Adds ConstructorInfo.GetGenericArguments() override returning an empty array for all constructors.
src/libraries/System.Runtime/tests/System.Reflection.Tests/ConstructorInfoTests.cs Adds xUnit coverage ensuring constructors’ GetGenericArguments() returns an empty array across representative types.

NativeAOT previously overrode ConstructorInfo.GetGenericArguments() to
throw NotSupportedException for desktop compat. Remove that override so
the new ConstructorInfo base implementation (returns empty array) is
used uniformly.

Update two tests that asserted the prior throw behavior to assert the
new empty-array contract:
- System.Reflection.Context CustomConstructorInfo test
- System.Reflection.MetadataLoadContext invariant test
  (TestConstructorInfoCommonInvariants is the upstream of several
  failing tests in TypeTests and ConstructorTests)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@steveisok steveisok added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label May 12, 2026
@steveisok
Copy link
Copy Markdown
Member Author

This is a breaking change that would need settled before merging.

Copy link
Copy Markdown
Member

@max-charlamb max-charlamb left a comment

Choose a reason for hiding this comment

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

lgtm

@steveisok steveisok removed the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label May 12, 2026
steveisok and others added 2 commits May 12, 2026 12:18
The System.Reflection.MetadataLoadContext tests target both
NetCoreAppCurrent and NetFrameworkCurrent. On .NET Framework,
ConstructorInfo.GetGenericArguments() still throws NotSupportedException
(this fix only ships in .NET Core). Guard the invariant check with
#if NET so the test asserts the appropriate behavior on each runtime.

This addresses the remaining failures in the Libraries_NET481 leg:
TestConstructors1, TestInvariantCode, TestMakeArray, TestInvariants,
TestMakeGenericType, and TestMakeMdArray (all upstream-rooted in
TestConstructorInfoCommonInvariants).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Per @max-charlamb's suggestion, add coverage for an open generic type
(GenericClassWithConstructor<>) in addition to the closed one.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 12, 2026 16:28
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

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

steveisok and others added 2 commits May 12, 2026 15:04
Per @jkotas's review, the public override on a public type needs to be
declared in the reference assembly so it appears in the public API
surface area.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Per @jkotas's review, the ConstructorInfo base class already defines
MemberType => MemberTypes.Constructor (and exposes it in the reference
assembly), so the override on RuntimeConstructorInfo.CoreCLR is
unnecessary.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@steveisok steveisok merged commit 214a9a4 into dotnet:main May 13, 2026
156 checks passed
@steveisok steveisok deleted the fix-constructorinfo-getgenericarguments branch May 13, 2026 11:15
Sign up for free to 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.

GetGenericArguments throws NotSupportedException on ConstructorInfo

4 participants