显然,在PowerShell(第3版)中并非所有$null都是相同的:
>function emptyArray() { @() }
>$l_t = @() ; $l_t.Count
0
>$l_t1 = @(); $l_t1 -eq $null; $l_t1.count; $l_t1.gettype()
0
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
>$l_t += $l_t1; $l_t.Count
0
>$l_t += emptyArray; $l_t.Count
0
>$l_t2 = emptyArray; $l_t2 -eq $null; $l_t2.Count; $l_t2.gettype()
True
0
You cannot call a method on a null-valued expression.
At line:1 char:38
+ $l_t2 = emptyArray; $l_t2 -eq $null; $l_t2.Count; $l_t2.gettype()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo …Run Code Online (Sandbox Code Playgroud) I've stumbled across an odd situation in PowerShell 5.1 (Windows 10) where ConvertTo-Json produces {} instead of null for an apparently null valued object:
[CmdletBinding()]
param()
function main() {
$foo1 = $null
Write-Verbose ("foo1 null? " + ($null -eq $foo1))
$foo2 = try{1/0}catch{}
Write-Verbose ("foo2 null? " + ($null -eq $foo2))
$foo3 = try{1/0}catch{$null}
return [PSCustomObject]@{"foo1"=$foo1; "foo2" = $foo2; "foo3" = $foo3} | ConvertTo-Json
}
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
main
Run Code Online (Sandbox Code Playgroud)
Running this cmdlet -verbose shows two apparently $null …