Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FixTypeName.GetReflectionType() to work when theTypeName instance represents a generic type definition within aGenericTypeName#24985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
daxian-dbw merged 6 commits intoPowerShell:masterfromdaxian-dbw:getreflectiontype
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -697,6 +697,26 @@ private bool VisitGenericTypeName(GenericTypeName genericTypeName)
if (foundType != null)
{
((ISupportsTypeCaching)genericTypeName).CachedType = foundType;

// When the generic type is in cache, we won't go through the code path that resolves
// 'genericTypeName.TypeName', which represents the generic type definition.
//
// - We could get the type definition by 'genericTypeName.TypeName.GetReflectionType()'
// in some cases, for example, when the generic type is 'System.Tuple[string, string]',
// calling 'GetReflectionType()' returns the 'System.Tuple' type.
//
// - But in much more other cases, calling that method won't return the type definition,
// for example, when the generic type is 'System.Collections.Generic.List[string]',
// the call returns 'null' because 'System.Collections.Generic.List' is not a valid type.
//
// In the latter cases, given that we already have the generic type, we can get the type
// definition and assign it to the cached type of 'genericTypeName.TypeName'.
var genericDefinition = genericTypeName.TypeName;
if (genericDefinition.GetReflectionType() is null)
{
((ISupportsTypeCaching)genericDefinition).CachedType = foundType.GetGenericTypeDefinition();
}

return true;
}

Expand Down
25 changes: 25 additions & 0 deletionstest/powershell/Language/Parser/Parsing.Tests.ps1
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -683,6 +683,31 @@ Describe "Additional tests" -Tag CI {
$result.EndBlock.Statements[0].PipelineElements[0].Expression.TypeName.FullName | Should -Be 'System.Tuple[System.String[],System.Int32[]]'
}

It "Should correctly set the cached type for 'GenericTypeName.TypeName' as needed when the generic type is found in cache" {
$tks = $null
$ers = $null
$Script = '[System.Collections.Generic.List[string]]'

## See https://github.com/PowerShell/PowerShell/issues/24982 for details about the issue.
$result = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$tks, [ref]$ers)
$typeExpr = $result.EndBlock.Statements[0].PipelineElements[0].Expression
$typeExpr.TypeName.FullName | Should -Be 'System.Collections.Generic.List[string]'
$typeExpr.TypeName.TypeName.FullName | Should -Be 'System.Collections.Generic.List'
$typeExpr.TypeName.TypeName.GetReflectionType() | Should -Not -BeNullOrEmpty
$typeExpr.TypeName.TypeName.GetReflectionType().FullName | Should -Be 'System.Collections.Generic.List`1'

$result2 = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$tks, [ref]$ers)
$typeExpr2 = $result2.EndBlock.Statements[0].PipelineElements[0].Expression
$typeExpr2.TypeName.FullName | Should -Be 'System.Collections.Generic.List[string]'
$typeExpr2.TypeName.TypeName.FullName | Should -Be 'System.Collections.Generic.List'
$typeExpr2.TypeName.TypeName.GetReflectionType() | Should -Not -BeNullOrEmpty
$typeExpr2.TypeName.TypeName.GetReflectionType().FullName | Should -Be 'System.Collections.Generic.List`1'

$Script = '[System.Tuple[System.String[],System.Int32[]]]'
$result3 = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$tks, [ref]$ers)
$result3.EndBlock.Statements[0].PipelineElements[0].Expression.TypeName.TypeName.GetReflectionType().FullName | Should -Be 'System.Tuple'
}

It "Should get correct offsets for number constant parsing error" {
$tks = $null
$ers = $null
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp