-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Steps to reproduce
There are few issues with casting to [System.Management.Automation.PSCustomObject]. Note the full name! The short name [PSCustomObject] works just fine, I guess it uses a different (type accelerator) code path.
First part
Try to create a [PSCustomObject] as described in a numerous of blog posts (example)
> $a = [PSCustomObject]@{a = 'a'; b = 'b'}
> $a
a b
- -
a b
> $a.GetType().FullName
System.Management.Automation.PSCustomObject
So far so good.
Now, try to do it with the full name
> [System.Management.Automation.PSCustomObject]@{a = 'a'; b = 'b'}
Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type
"System.Management.Automation.PSCustomObject".
At line:1 char:2
+ [System.Management.Automation.PSCustomObject]@{a = 'a'; b = 'b'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Expected behavior
Conversion succeed.
Actual behavior
> [System.Management.Automation.PSCustomObject]@{a = 'a'; b = 'b'}
Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type
"System.Management.Automation.PSCustomObject".
At line:1 char:2
+ [System.Management.Automation.PSCustomObject]@{a = 'a'; b = 'b'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Second part
Now try to cast the original object $a
> [System.Management.Automation.PSCustomObject]$a
Cannot convert the "@{a=a; b=b}" value of type "System.Management.Automation.PSCustomObject" to type
"System.Management.Automation.PSCustomObject".
At line:1 char:2
+ [System.Management.Automation.PSCustomObject]$a
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
The error message is pretty weird, since no actual conversion should be required.
Note: this affects UX, because auto-completion on [pscu will expend it into [System.Management.Automation.PSCustomObject and not [PSCustomObject.
This is also inconsistent with [pso that expends into [psobject and not [System.Management.Automation.PSObject]
Also, note that this works fine
> [System.Management.Automation.PSObject]@{a = 'a'; b = 'b'}
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 6.0.0-alpha
PSEdition Core
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 3.0.0.0
GitCommitId v6.0.0-alpha.10
CLRVersion
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1 cc @jpsnover