如何在Powershell 5类中声明静态成员?

Avn*_*ner 8 powershell static class powershell-5.0

如果我有一个PS 5类,我想要一个静态成员 - 我该如何声明它?

Class Foo {
  [string] $Bar = 'static member'
}
Run Code Online (Sandbox Code Playgroud)

所以我可以在没有像这样的实例的情况下引用它

Write-Host [Foo]::Bar
Run Code Online (Sandbox Code Playgroud)

这可能吗?

iCo*_*dez 12

PowerShell 5 static为此添加了一个关键字:

static [string] $Bar = 'static member'
Run Code Online (Sandbox Code Playgroud)

演示:

PS > class Foo {
>>     static [string] $Bar = 'static member'
>> }   
PS > [Foo]::Bar
static member
PS >
Run Code Online (Sandbox Code Playgroud)