显然,在 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 : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
>$l_t += $l_t2; $l_t.Count
0
>$l_t3 = $null; $l_t3 -eq $null;$l_t3.gettype()
True
You cannot call a method on a null-valued expression.
At line:1 char:32
+ $l_t3 = $null; $l_t3 -eq $null;$l_t3.gettype()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
>$l_t += $l_t3; $l_t.count
1
>function addToArray($l_a, $l_b) { $l_a += $l_b; $l_a.count }
>$l_t = @(); $l_t.Count
0
>addToArray $l_t $l_t1
0
>addToArray $l_t $l_t2
1
那么如何以及为什么
$l_t2
不同于 $l_t3
?特别是$l_t2
真的$null
或不?请注意 $l_t2
不是空数组( $l_t1
是,而 $l_t1 -eq $null
不返回任何内容,正如预期的那样),但也不是真正的 $null
, 喜欢 $l_t3
.特别是$l_t2.count
返回 0 而不是错误,此外,添加 $l_t2
至 $l_t
行为就像添加一个空数组,而不是添加 $null
.为什么$l_t2
当它传入函数 $null
时,突然似乎变成了“更多 addToArray
”作为参数??????任何人都可以解释这种行为,或者向我指出可以解释它的文档吗?
编辑:
下面 PetSerAl 的回答是正确的。 I have also found this stackOverflow post on the same issue.
Powershell 版本信息:
>$PSVersionTable
Name Value
---- -----
WSManStackVersion 3.0
PSCompatibleVersions {1.0, 2.0, 3.0}
SerializationVersion 1.1.0.1
BuildVersion 6.2.9200.16481
PSVersion 3.0
CLRVersion 4.0.30319.1026
PSRemotingProtocolVersion 2.2
最佳答案
In particular, is
$l_t2
really$null
or not?
$l_t2
不是 $null
,但是一个 [System.Management.Automation.Internal.AutomationNull]::Value
.它是 PSObject
的特例.当管道返回零个对象时返回它。这就是你可以检查它的方法:$a=&{} #shortest, I know, pipeline, that returns zero objects
$b=[System.Management.Automation.Internal.AutomationNull]::Value
$ReferenceEquals=[Object].GetMethod('ReferenceEquals')
$ReferenceEquals.Invoke($null,($a,$null)) #returns False
$ReferenceEquals.Invoke($null,($a,$b)) #returns True
我打电话
ReferenceEquals
通过反射防止来自 AutomationNull
的转换通过 PowerShell 设置为 $null。
$l_t1 -eq $null
returns nothing
对我来说,它返回一个空数组,正如我所期望的那样。
$l_t2.count
returns 0
这是一个 new feature of PowerShell v3 :
You can now use Count or Length on any object, even if it didn’t have the property. If the object didn’t have a Count or Length property, it will will return 1 (or 0 for $null). Objects that have Count or Length properties will continue to work as they always have.
PS> $a = 42 PS> $a.Count 1
And why does
$l_t2
suddenly seem to become "more$null
" when it gets passed in the the functionaddToArray
as a parameter???????
似乎 PowerShell 会转换
AutomationNull
至 $null
在某些情况下,例如调用 .NET 方法。在 PowerShell v2 中,即使保存 AutomationNull
到一个变量,它被转换为 $null
.
关于powershell - 这两个 $null 值为什么以及如何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55361015/