powershell - 什么是 List`1 对象?

标签 powershell generics 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]]

最佳答案

vonPryz在评论中提供了关键指针:List`1 .NET 对 generic type 的表示命名为 List与 arity ( ` ) 1 ,即具有 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]] ,但是可以通过两种方式简化:

  • arity 指标,`1 , 可以省略,因为 [...] 中的类型参数隐含了元数, [string] .
  • 鉴于只有 1 个类型参数,您可以省略外部 [...]围绕类型参数列表。

  • 因此,您可以只使用 System.Collections.Generic.List[string] ,或者,表示为 PowerShell 类型文字 ( [...] ),[System.Collections.Generic.List[string]]
    可选阅读:在 PowerShell 中缩短类型名称和文字:[System.Collections.Generic.List[string]]有点笨拙,有两种方法可以缩短它:
  • PowerShell 允许您省略 System.任何类型的命名空间的一部分 , 所以 [Collections.Generic.List[string]]也可以。
  • 电源 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]]
    

  • 此外,PowerShell 有 内置类型加速器 对于某些经常使用的类型,它们是引用特定类型的单组件名称,而无需指定其来源 namespace ;例如,[xml][System.Xml.XmlDocument] 的类型加速器.
    This TechNet blog post显示可以使用以下命令来列出所有内置类型加速器 :
    [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::
      get.GetEnumerator() | Sort-Object Key
    
    TheIncorrigible1指出,你甚至可以使用 ::Add() 定义您自己的类型加速器方法 ;例如,以下命令定义 [cmdinfo]作为 [System.Management.Automation.CommandInfo] 类型的加速器:
    [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::
          Add('cmdinfo', [System.Management.Automation.CommandInfo])
    
    新的加速器将在 session 全局范围内可用(但仅适用于当前 session ),即使调用是从子范围进行的。
    也就是说,有充分的理由不这样做 :
  • System.Management.Automation.TypeAccelerators不是公共(public)类型,因此它不是 PowerShell 的公共(public) API 的一部分,因此不能保证将来会存在(以目前的形式)。
  • 因此, using namespace方法更可取 .
  • 关于powershell - 什么是 List`1 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54961238/

    相关文章:

    iOS - 如何使用泛型在 Swift 中编写 DecodeHelper 类?

    c# - 通用克隆实例

    Powershell 脚本将新文件夹创建为 @{Folder=Test-folder}/

    c# - 如何将此 PowerShell 脚本转换为 C#? - 获取项目属性

    c# - 如何对通用列表进行排序?

    types - 根据系统切换数据类型?

    haskell - 为什么 `(\x y -> x + y) @Integer`失败,但 `(+) @Integer`成功?

    functional-programming - 什么是欣德利米尔纳?

    arrays - PowerShell不会将空数组作为数组返回

    .net - PowerShell 和 StringBuilder