【问题标题】:What do you call these? [array][string][int]你怎么称呼这些? [数组][字符串][整数]
【发布时间】:2021-04-07 23:01:52
【问题描述】:

这些叫什么?在 powershell 中编写脚本时,我可以使用它们来设置或转换变量的数据类型,但是这个术语是什么?这些有官方文档吗?

例子:

$var = @("hello","world")
If ($var -is [array]) { write-host "$var is an array" }

【问题讨论】:

  • 它们基本上是对 dotnet 类型的引用——例如[string].GetType().Name 返回RuntimeType
  • 这是一个棘手的问题,因为 Powershell 会为您处理关于它变成什么的问题。 posh 知道,不需要向它强制转换数据类型。比如$Var = 2就会是一个int的数据类型。与$Var = "string" 相同,数据类型为[string]

标签: .net powershell terminology


【解决方案1】:

Don Cruickshank's helpful answer 提供了一个难题,但让我尝试给出一个全面的概述:

本身[<fullTypeNameOrTypeAccelerator>] 表达式是一个类型文字,即对 .NET 类型的引用 em> 以System.Reflection.TypeInfo 实例的形式,它是对它所代表的类型的反射的丰富来源。

<fullTypeNameOrTypeAccelerator> 可以是 .NET 类型的全名(例如,[System.Text.RegularExpressions.Regex] - 可以选择省略 System. 前缀 ([Text.RegularExpressions.Regex]) 或 PowerShell 的名称 type accelerator(例如,@987654338 @)

类型文字也用于以下结构

  • 作为casts,如果可能,将 (RHS[1]) 操作数强制为指定类型:

    [datetime] '1970-01-01'  # convert a string to System.DateTime
    
    • 请注意,例如,PowerShell 强制转换比 C# 灵活得多,并且类型转换经常发生隐式 - 有关详细信息,请参阅 this answer。相同的规则适用于下面列出的所有其他用途。
  • 作为类型约束

    • 指定函数或脚本中parameter 变量的类型

      function foo { param([datetime] $d) $d.Year }; foo '1970-01-01'
      
    • 锁定类型 常规 variable 用于所有未来的分配:[2]

      [datetime] $foo = '1970-01-01'
      # ...
      $foo = '2021-01-01' # the string is now implicitly forced to [datetime] 
      
  • 作为-is-as 运算符的RHS,对于type tests and conditional conversions

    • -is 不仅测试确切类型,还测试派生类型以及接口实现:

      # Exact type match (the `Get-Date` cmdlet outputs instances of [datetime])
      (Get-Date) -is [datetime]  # $true
      
      # Match via a *derived* type:
      # `Get-Item /` outputs an instance of type [System.IO.DirectoryInfo],
      # which derives from [System.IO.FileSystemInfo]
      (Get-Item /) -is [System.IO.FileSystemInfo] # $true
      
      # Match via an *interface* implementation:
      # Arrays implement the [System.Collections.IEnumerable] interface.
      1..3 -is [System.Collections.IEnumerable] # true
      
    • -as 将 LHS 实例转换为 RHS 类型的实例如果可能,否则返回 $null

      '42' -as [int] # 42
      
      'foo' -as [int] # $null
      

[1] 在算子和数学方程的上下文中,常用的首字母缩写为 LHS 和 RHS,分别指left-hand sideright-hand side 操作数,分别。

[2] 从技术上讲,parameterregular 变量之间没有真正的区别:类型约束在两种情况下的作用方式相同,但参数变量在调用时自动绑定(分配给)后,通常不会再次分配给

【讨论】:

  • 很好的答案,像往常一样!如果可以的话,我想添加一篇好文章来更多地了解这个主题 blog.kotlin-academy.com/…,当然还有来自 MS Docs 的系统命名空间 docs.microsoft.com/en-us/dotnet/api/system?view=net-5.0
  • 什么是 RHS?那么,对于 powershell cmdlet,“幕后”,实际的 .Net 类型会撒谎吗?例如您引用的完全匹配的那些:Get-Date 实际上只是[DateTime],但是在 Powershell 语法中?当我从管道 cmdlet 到 Get-Member 中获取数据类型时仍然感到困惑,它只是抓取那些位于这些类型中的对象,还是它本身是实际类型? -如果这没有意义,请原谅我
  • @AbrahamZinala,关于 LHS 和 RHS:请参阅我刚刚添加的脚注。我不确定我是否理解您的另一个问题:Get-Date 是一个 cmdlet,它输出 [datetime] 的实例。您将 instances 传送到 Get-Member,然后它会告诉您它们的 type
  • Get-Date 正是 PowerShell 必须访问系统命名空间中的 DateTime Struct 的方式。 PS 原生的几乎所有内容(或不确定的所有内容,cmdlet、数据结构、类、类型等)都来自 C# / .NET Framework / .NET Core。 mklement0 大概可以在这里给我们一个更准确的答案。但从某种意义上说,是的,Get-Date 正在访问System.DateTime 并帮助您操纵它@AbrahamZinala
  • 不客气,@AbrahamZinala;让我尝试换一种说法:在表达式 (Get-Date) -is [datetime] 中,(Get-Date) 是对 Get-Date cmdletcall 以及该 cmdlet 输出的任何内容 - 其中恰好是 [datetime] 实例 - 成为 -is 运算符的 LHS。是的,PowerShell 中的所有对象都是 .NET 类型的实例。
【解决方案2】:

它被称为cast 操作符。官方文档在about_operators中使用了这个术语。

强制转换运算符 [ ]

将对象转换或限制为指定类型。如果对象 无法转换,PowerShell 会产生错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 2022-08-18
    相关资源
    最近更新 更多