I’ve asked this question to colleagues and of course google. I’m assuming it has something to do with it being a type accelerator, but from what I’ve read my understanding is that type accelerators help you to not have to type the full .Net type. So why can’t I do it the long way? I’m looking for actual technical reasons, not “because Bruce Payette said so” or the like.
This
$var = [System.Management.Automation.PSCustomObject]@{
Name = 'test'
Admin = $true
}
results in error
Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type "System.Management.Automation.PSCustomObject".
I’ve tried your cast inside a Trace-Command block… and getting no more useful info.
I’ve assumed also it might be a parser thing (maybe Bruce checked for this specific cast only for the type accelerator name, and not for the full base type, or the System namespace but not System.Management.Automation, or whatever …)
But it is not the parser. It seems something inside .NET, as doing it in two steps also fails the same way :
$var1 = @{
Name = ‘test’
Admin = $true
} # OK
$Var2 = [System.Management.Automation.PSCustomObject]$Var1 # Same error
That is exactly the issue, it tabbed completed and didn’t work. I am curious (and annoyed) with my favorite search engine that I wasn’t able to locate this. Thank you so much for spending the time to find this.
// We can't use the normal PSConvertBinder because it is strongly typed, and we
// play some funny games with the PSCustomObject type (externally, it's just an
// alias for PSObject, internally we do other stuff with it.)