全局变量 一起 class Set-StrictMode -version 2 ,我该怎么做?
class
Set-StrictMode -version 2
Set-StrictMode -version 2 class MyClass{ [string] MyMethod(){ $x = $ErrorActionPreference ...
Powershell抱怨
$x = $ErrorActionPreference Variable is not assigned in the method.
这种分配在模式2中被认为是有害的。 $ErrorActionPreference global scope .
$ErrorActionPreference
您的示例在严格模式2中有效,应该如下所示:
Set-StrictMode -Version 2 class MyClass { [string] MyMethod(){ $x = $global:ErrorActionPreference return $x } } [MyClass]::new().MyMethod()