什么是List`1对象?

lit*_*lit 2 generics powershell types

System.IO.FileInfo具有Target成员.

使用Get-Item -Path * -Include 't.txt' | Get-Member显示它有一个Target成员CodeProperty.

使用GetType()显示它是一个List`1

C:>Get-Item -Path * -Include 't.txt' | ForEach-Object { $_.Target.GetType() }

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     List`1                                   System.Object

C:>Get-Item -Path * -Include 't.txt' | % { $_.Target.GetType() | % { $_.FullName } }
System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 5

vonPryz在注释中提供了关键指针:List`1.NET 用arity()命名的泛型类型的表示,即具有1个类型参数的泛型类型List`1.

(`在此上下文中的使用与PowerShell `作为转义字符的使用无关).

在您的情况下,System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]表示通用类型已使用类型关闭(实例化)System.String.

保留程序集限定(mscorlib, Version = ...),等效的PowerShell表示System.Collections.Generic.List`1[[string]],可以通过两种方式简化:

  • 的元数指示器,`1可以省略,因为该元数是通过在类型参数暗示[...],[string].
  • 鉴于只有一个类型参数,您可以省略[...]类型参数列表周围的外部.

因此,您可以使用just System.Collections.Generic.List[string],或表示为PowerShell 类型的literal([...]),[System.Collections.Generic.List[string]]


可选读取:缩短PowerShell中的类型名称和文字:

[System.Collections.Generic.List[string]] 有点笨拙,有两种方法可以缩短它:

  • PowerShell允许您省略System.任何类型的命名空间部分,因此[Collections.Generic.List[string]]也可以.

  • PowerShell v5 +提供了using namespace声明,类似于C#的using声明:

    # Note:
    #  * `using namespace` must be at the *start* of the script (potentially
    #    preceded by other `using` statements and comments only)
    #  * The 'System.' part of a namespace must *not* be omitted.
    using namespace System.Collections.Generic
    
    [List[string]] # short for: [System.Collections.Generic.List[string]]
    
    Run Code Online (Sandbox Code Playgroud)

此外,PowerShell还为某些常用类型提供内置类型加速器,这些类型是引用特定类型的单组件名称,无需指定其原始名称空间; 例如,[xml]是一种类型加速器[System.Xml.XmlDocument].

此TechNet博客文章显示您可以使用以下命令列出所有内置类型加速器:

[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::
  get.GetEnumerator() | Sort-Object Key
Run Code Online (Sandbox Code Playgroud)

正如TheIncorrigible1指出的那样,您甚至可以使用该::Add()方法定义自己的类型加速器 ; 例如,以下命令定义[cmdinfo]为类型的加速器[System.Management.Automation.CommandInfo]:

[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::
      Add('cmdinfo', [System.Management.Automation.CommandInfo])
Run Code Online (Sandbox Code Playgroud)

即使从子范围进行调用,新加速器也可以全局会话(但仅适用于当前会话).