Write-Error 的输出在具有返回值的方法中被省略(即不是 void):
Class MyClass {
[void]MethodWithoutReturnValue([string]$Msg) {
Write-Error "MyClass.MethodWithoutReturnValue(): $Msg"
}
[bool]MethodWithReturnValue([string]$Msg) {
Write-Error "MyClass.MethodWithReturnValue(): $Msg"
$this.MethodWithoutReturnValue("this won't work either")
return $true
}
}
[MyClass]$obj = [MyClass]::new()
$obj.MethodWithoutReturnValue('this error will show up')
[bool]$Result = $obj.MethodWithReturnValue('this error will NOT show up')
我期待三个错误消息,但只得到一个。请注意,从 bool 方法调用 void 方法也会忽略输出,就好像调用堆栈以某种方式“中毒”了一样。是的(虽然在这个例子中没有显示)调用 void 方法的 void 方法有效。
有人可以解释一下这种行为还是我只是发现了一个错误?