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

How to declare a static member in a Powershell 5 class?

如果我有一个 PS 5 class 并且我想要其中的静态成员 - 我该如何声明它?

Class Foo {
  [string] $Bar = 'static member'
}

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

Write-Host [Foo]::Bar

这可能吗?

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

static [string] $Bar = 'static member'

演示:

PS > class Foo {
>>     static [string] $Bar = 'static member'
>> }   
PS > [Foo]::Bar
static member
PS >