小编Mos*_*ini的帖子

PowerShell:为什么一个参数上的[Parameter(Mandatory = $ True)]影响另一个参数的类型转换行为?

有人知道为什么参数$ a上的强制属性会影响参数$ b的类型转换行为吗?在示例中,数组应强制转换为字符串。

function test ([Parameter(Mandatory = $True)] [string] $a, [string] $b) {
  $a; $b
}
$b = "a", "b", "c"
test -a "my string" -b $b
Run Code Online (Sandbox Code Playgroud)

执行此代码块时,将产生错误:test:无法处理参数'b'上的参数转换。无法将值转换为类型System.String。在线:1字符:31

如果我从$ a中删除了Mandatory属性,它可以正常工作:

function test ([string] $a, [string] $b) {
  $a; $b
}
$b = "a", "b", "c"
test -a "my string" -b $b
Run Code Online (Sandbox Code Playgroud)

预先感谢您的反馈

arrays string powershell casting

3
推荐指数
1
解决办法
59
查看次数

标签 统计

arrays ×1

casting ×1

powershell ×1

string ×1