You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why is ([System.Collections.ObjectModel.Collection[psobject]]1)[0] a string? #7154
>$PSVersionTable
Name Value
---------
PSVersion 6.1.0-preview.688
PSEdition Core
GitCommitId v6.1.0-preview.688
OS Microsoft Windows 6.3.9600
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
It looks like { Write-Object $obj -NoEnumerate }.Invoke() is a more reliable way to convert to [Collection[psobject]]. Or at least is was before #2038, and (hopefully) will be again once those issues are corrected. In the meantime Write-Output2 from 6451(comment) seems to work.
If you're not concerned about the intermediate, transient [object[]] array that is invariably constructed, you can also do [System.Collections.Generic.List[object]] @($obj)
I'm not sure this really is deliberate, I think another explanation is that this is just a symptom of PowerShell being especially permissive with conversions.
Removed a incorrect example showing a different error. Was my fault.
#Shows that the original creation of the psobject is equivalent
echo '(New-Object -TypeName PSObject -ArgumentList @(1)) -eq [psobject]1)'
(New-Object -TypeName PSObject -ArgumentList @(1)) -eq [psobject]1
orig_notworking - String
orig_working - Int32
#Removed output from my mistake code above from this list
(New-Object -TypeName PSObject -ArgumentList @(1)) -eq [psobject]1)
True
notworking - String
working - Int32
Sorry for the terrible code and the old post revival, but I got interested. It feels like the constructor and/or assignment operator is doing something weird.
Ohhh nice catch. I'll remove that one example. Still interesting that the add doesn't convert the int to string but instantiating it using the constructor with initial values does.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
Activity
mklement0 commentedon Jun 24, 2018
I have no explanation, but 2 quick additional observations:
It only happens when you cast a scalar;
([System.Collections.ObjectModel.Collection[psobject]] (, 1))[0].GetType().NameyieldsInt32, as expected.It also happens with
[System.Collections.Generic.List[object]]; by contrast:[System.Collections.ArrayList]fails,[object[]]alx9r commentedon Jun 24, 2018
It looks like
{ Write-Object $obj -NoEnumerate }.Invoke()is a more reliable way to convert to[Collection[psobject]]. Or at least is was before #2038, and (hopefully) will be again once those issues are corrected. In the meantimeWrite-Output2from 6451(comment) seems to work.mklement0 commentedon Jun 24, 2018
If you're not concerned about the intermediate, transient
[object[]]array that is invariably constructed, you can also do[System.Collections.Generic.List[object]] @($obj)PetSerAl commentedon Jun 24, 2018
It seems that given conversion covered here:
PowerShell/src/System.Management.Automation/engine/LanguagePrimitives.cs
Line 5399 in bbb4f2e
And
ConvertNumericIConvertiblespecifically do conversion thru string:PowerShell/src/System.Management.Automation/engine/LanguagePrimitives.cs
Lines 3858 to 3859 in bbb4f2e
alx9r commentedon Jun 24, 2018
Interesting. That is clearly deliberate. But why do that?
lzybkr commentedon Jun 25, 2018
I'm not sure this really is deliberate, I think another explanation is that this is just a symptom of PowerShell being especially permissive with conversions.
Based on this check:
PowerShell/src/System.Management.Automation/engine/LanguagePrimitives.cs
Line 5395 in bbb4f2e
KnowledgeNerd commentedon Jun 3, 2022
I feel like this isn't purposeful and some if going on weird here. This is a PowerShell code showing the same and other weird issues.
Code
$orig_notworking = ([System.Collections.ObjectModel.Collection[psobject]]1)
echo "orig_notworking - $($orig_notworking[0].GetType().Name)"
$orig_working = (New-Object -TypeName System.Collections.ObjectModel.Collection[psobject])
$orig_working.Add([psobject]1)
echo "orig_working - $($orig_working[0].GetType().Name)"
Removed a incorrect example showing a different error. Was my fault.
#Shows that the original creation of the psobject is equivalent
echo '(New-Object -TypeName PSObject -ArgumentList @(1)) -eq [psobject]1)'
(New-Object -TypeName PSObject -ArgumentList @(1)) -eq [psobject]1
$notworking = ([System.Collections.ObjectModel.Collection[psobject]](New-Object -TypeName PSObject -ArgumentList @(1)))
echo "notworking - $($notworking[0].GetType().Name)"
$working = (New-Object -TypeName System.Collections.ObjectModel.Collection[psobject])
$working.Add((New-Object -TypeName PSObject -ArgumentList @(1)))
echo "working - $($working[0].GetType().Name)"
Output
orig_notworking - String
orig_working - Int32
#Removed output from my mistake code above from this list
(New-Object -TypeName PSObject -ArgumentList @(1)) -eq [psobject]1)
True
notworking - String
working - Int32
Sorry for the terrible code and the old post revival, but I got interested. It feels like the constructor and/or assignment operator is doing something weird.
PetSerAl commentedon Jun 3, 2022
KnowledgeNerd, you assign to
$orig_work_but_not_workingthe result of callingAdd, which isvoid, and not the collection.KnowledgeNerd commentedon Jun 3, 2022
Ohhh nice catch. I'll remove that one example. Still interesting that the add doesn't convert the int to string but instantiating it using the constructor with initial values does.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
KnowledgeNerd commentedon Nov 17, 2023
Gotta say I revisited this again and was appalled at my code... But I still think something is weird.
Check out this ~slightly~ better and cleaner code to see what's going on:
Output
microsoft-github-policy-service commentedon Nov 25, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.