Add regression tests for generic type parameter reflection properties#125581
Draft
Add regression tests for generic type parameter reflection properties#125581
Conversation
Add tests to verify that generic type parameters (e.g., T from List<T>) correctly report IsGenericType=false, IsGenericParameter=true, FullName=null, and that DeclaringType correctly points back to the generic type definition. These tests document the by-design circular reference between DeclaringType and GetGenericArguments() that user code must handle by checking IsGenericParameter. Addresses dotnet/runtime#XXXXX - the reported StackOverflowException is caused by user code that recursively walks both DeclaringType and GetGenericArguments() without checking IsGenericParameter, not by a runtime bug. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
stephentoub
March 15, 2026 15:25
View session
This was referenced Mar 15, 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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds regression tests to verify the correct behavior of reflection properties for generic type parameters (e.g.,
TfromList<T>), specifically addressing the scenario described in the issue where processing unbound generic types liketypeof(List<>)through reflection can lead toStackOverflowException.Root Cause Analysis
The reported
StackOverflowExceptionis not a runtime bug. It is caused by user code that recursively walks bothType.DeclaringTypeandType.GetGenericArguments()without checkingType.IsGenericParameter, creating an infinite cycle:typeof(List<>).GetGenericArguments()returns[T](the type parameter)T.DeclaringTypereturnstypeof(List<>)(the declaring generic type definition)typeof(List<>)again leads back to step 1 → infinite recursionThis circular reference between
DeclaringTypeandGetGenericArguments()is by design. The correct pattern is to checktype.IsGenericParameterbefore recursing throughDeclaringType, as demonstrated in the runtime's ownTypeNameBuilder.cs:Changes
Added two test methods to
TypeTests.cs:GenericTypeParameter_Properties(Theory with 6 cases): Verifies that generic type parameters correctly reportIsGenericType=false,IsGenericParameter=true,FullName=null, expectedName, and correctDeclaringTypefor parameters fromList<>,Dictionary<,>,Outside<>, andOutside<>.Inside<>.GenericTypeDefinition_GetGenericArguments_DeclaringTypeCycle: Documents and verifies the by-design circular reference betweenDeclaringTypeandGetGenericArguments()for both single-parameter (List<>) and multi-parameter (Dictionary<,>) generic type definitions.Test Results
All 7 new tests pass. Full test suite (68,089 tests) passes with 0 failures.