在 PowerShell 5 中是否可以为 class 声明通用属性?

In PowerShell 5 is it possible to declare generic properties for a class?

PowerShell version 5 introduced the Class keyword, making it easier to create custom classes in PowerShell. The announcement only provides a brief summary on properties:

All properties are public. Properties require either a newline or semicolon. If no object type is specified, the property type is object.

到目前为止一切顺利。这意味着我可以轻松创建如下所示的 class:

Class Node
{
    [String]$Label
    $Nodes
}

我 运行 遇到的问题是,如果不为 $Nodes 指定类型,它默认为 System.Object。我的目标是使用 System.Collections.Generic.List 类型,但到目前为止还没有想出如何去做。

Class Node
{
    [String]$Label
    [System.Collections.Generic.List<Node>]$Nodes
}

以上导致一连串的问题:

At D:\Scripts\Test.ps1:4 char:36
+     [System.Collections.Generic.List<Node>]$Nodes
+                                    ~
Missing ] at end of attribute or type literal.
At D:\Scripts\Test.ps1:4 char:37
+     [System.Collections.Generic.List<Node>]$Nodes
+                                     ~
Missing a property name or method definition.
At D:\Scripts\Test.ps1:4 char:5
+     [System.Collections.Generic.List<Node>]$Nodes
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Missing closing '}' in statement block or type definition.
At D:\Scripts\Test.ps1:5 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EndSquareBracketExpectedAtEndOfAttribute

这引出了我的问题:如何在 PowerShell 5 中为 属性 使用泛型?

在设计我的问题时,我偶然发现了一个 answer which details how to create Dictionary objects in PowerShell 2:

$object = New-Object 'system.collections.generic.dictionary[string,int]'

需要特别注意的是,通用声明中没有使用 <>,而是使用 []。将我的 class 声明切换为使用 square brackets instead of angle brackets 解决了我的问题:

Class Node
{
    [String]$Label
    [System.Collections.Generic.List[Node]]$Nodes
}