Fix generic List conversion from Boolean scalar - Issue #17731#27079
Open
Thatgfsj wants to merge 1 commit intoPowerShell:masterfrom
Open
Fix generic List conversion from Boolean scalar - Issue #17731#27079Thatgfsj wants to merge 1 commit intoPowerShell:masterfrom
Thatgfsj wants to merge 1 commit intoPowerShell:masterfrom
Conversation
Issue: PowerShell#17731 - Cannot directly construct a generic list from a Boolean scalar The ConvertViaIEnumerableConstructor.Convert method was directly adding the scalar value to the list without converting it to the element type first. This worked for types like int where the value's runtime type matches the expected element type, but failed for bool because the PowerShell Boolean wrapper type is different from System.Boolean. Fix: Use LanguagePrimitives.ConvertTo to convert the scalar value to the element type before adding to the list, similar to how non-scalar values are handled. Added tests for: - [List[bool]]$true - [List[bool]]$false - [List[bool]]$boolVar - [List[psobject]]$true
Author
|
@microsoft-github-policy-service agree |
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
Fixes Issue #17731 - Cannot directly construct a generic list from a Boolean scalar
Problem
The following statements return an error:
\\powershell
[System.Collections.Generic.List[bool]]$False
[System.Collections.Generic.List[bool]]$True
[System.Collections.Generic.List[psobject]]$True
\\
While these statements work fine:
\\powershell
[System.Collections.Generic.List[int]]3
[System.Collections.Generic.List[string]]'Test'
[Array]$True
\\
Root Cause
The \ConvertViaIEnumerableConstructor.Convert\ method was directly adding the scalar value to the list without converting it to the element type first. This worked for types like \int\ where the value's runtime type matches the expected element type, but failed for \�ool\ because the PowerShell Boolean wrapper type differs from \System.Boolean.
Fix
Use \LanguagePrimitives.ConvertTo\ to convert the scalar value to the element type before adding to the list, similar to how non-scalar values are handled.
Changed Files
Testing
Added tests for:
Fixes #17731