为什么 $A+$B 和 $A,$B 与测试路径的交互方式不同
Why does $A+$B and $A,$B interact differently with Test-Path
我有两个数组 $A
和 $B
都可能是空的。
$A = $B = @()
这个有效:
$A+$B | Test-Path
这不起作用:
$A,$B | Test-Path
和returns错误:
Test-Path : Cannot bind argument to parameter 'Path' because it is an empty array.
我本以为这两个表达式都会失败,因为 +
运算符将一个空数组添加到另一个空数组,这意味着生成的数组仍然是空的?
查看两种方法的整体类型,发现它们是同一类型。
PS Y:\> $E = $A+$B
PS Y:\> $E.getType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS Y:\> $F = $A,$B
PS Y:\> $F.getType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
那么为什么 $A+$B
& $A,$B
与 Test-Path
的交互方式不同?
$A+$B | ...
在将结果数组传递给管道之前连接 $A
和 $B
。然后管道展开(仍然是空的)数组,所以你得到 $null
并且 Test-Path
永远不会被调用。
$A,$B | ...
在将其传递到管道之前构造一个包含两个嵌套数组的数组。然后管道展开外部数组并将每个元素(空数组 $A
和 $B
)提供给 Test-Path
,从而导致您观察到的错误。
基本上你在前者中做 $A+$B → @()
,在后一种情况中做 $A,$B → @(@(), @())
。
我有两个数组 $A
和 $B
都可能是空的。
$A = $B = @()
这个有效:
$A+$B | Test-Path
这不起作用:
$A,$B | Test-Path
和returns错误:
Test-Path : Cannot bind argument to parameter 'Path' because it is an empty array.
我本以为这两个表达式都会失败,因为 +
运算符将一个空数组添加到另一个空数组,这意味着生成的数组仍然是空的?
查看两种方法的整体类型,发现它们是同一类型。
PS Y:\> $E = $A+$B
PS Y:\> $E.getType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS Y:\> $F = $A,$B
PS Y:\> $F.getType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
那么为什么 $A+$B
& $A,$B
与 Test-Path
的交互方式不同?
$A+$B | ...
在将结果数组传递给管道之前连接 $A
和 $B
。然后管道展开(仍然是空的)数组,所以你得到 $null
并且 Test-Path
永远不会被调用。
$A,$B | ...
在将其传递到管道之前构造一个包含两个嵌套数组的数组。然后管道展开外部数组并将每个元素(空数组 $A
和 $B
)提供给 Test-Path
,从而导致您观察到的错误。
基本上你在前者中做 $A+$B → @()
,在后一种情况中做 $A,$B → @(@(), @())
。