powershell - char[] 和 char 有什么区别?

标签 powershell

如果我使用 [char] 数据类型,它要求值是一个字符,例如

[char]"a"
a
因为如果我将它与多个字符一起使用,它会给我一个错误:
[char]"ab"
Cannot convert value "ab" to type "System.Char". Error: "String must be exactly one
character long."
At line:1 char:1
+ [char]"ab"
+ ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocation
但是,如果我使用
[char[]]"ab"
我得到输出
a
b
如果我比较 Get-Member 两者,我没有得到任何结果:
PS C:\Users\nijoh> Compare-Object -ReferenceObject $([char] | gm) -DifferenceObject $([char[]] | gm) -PassThru
PS C:\Users\nijoh>
但我可以看到它们是两种不同的类型,因为它们的显示方式不同:
PS C:\Users\nijoh> ([char[]]"a").GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Char[]                                   System.Array


PS C:\Users\nijoh> ([char]"a").GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Char                                     System.ValueType
那么Powershell中的[char][char[]]数据类型有什么区别呢?

最佳答案

在 PowerShell 中,类型名称包含在 [...] 中是一个类型字面量,即表示命名的 .NET 类型的对象。
与此无关,在 [...] 内部, []紧跟在类型名称之后的代表包含该类型元素的数组; [...] 中使用的符号概述,见 this answer .
所以:

  • [char]指类型System.Char (System. 前缀在 PowerShell 中通常是可选的;此外 - 就像这里的情况一样 - PowerShell 有一组固定的类型加速器,允许您对位于任何命名空间中的类型使用简单名称 - 请参阅 this answer )。
  • 要获取给定类型文字的完整(命名空间限定).NET 类型名称,请使用 [...].FullName ;例如,[char].FullName产量 System.Char

  • [char[]]指一个数组类型,其元素的类型为 char (System.Char); [char[]].FullName产量 System.Char[] .

  • Since if I use it with more than one character it will give me an error ([char]"ab")


    PowerShell 没有 [char]文字,只有字符串 ( [string] ) 文字。当您将字符串转换为 [char] - 表示单个字符 - 只接受单个字符串;例如[char] 'a'有效,但 [char] 'ab'[char] "ab"没有。

    However if I use [char[]]"ab" ...


    将字符串转换为字符数组会返回由字符串的各个字符组成的字符数组。
    换句话说:[char[]] "ab"相当于"ab".ToCharArray() .

    If I compare Get-Member on both, I get no result:


    原因是 Get-Member对输入对象的类型进行操作 ,并且如果输入对象是类型文字 - 例如 [char][char[]] - 检查它们的类型,即 System.RuntimeType ,一种派生自 System.Reflection.TypeInfo 的非公共(public) PowerShell 类型,它描述了一个 .NET 类型。
    换句话说:所有类型文字都通过管道传送到 Get-Member将产生相同的输出,无论它们引用什么特定类型 ,因为它是唯一且唯一的类型描述类型,其成员正在被报告。
    因此,使用 Compare-ObjectGet-Member可以预见的是,使用不同类型文字的调用不会产生任何输出,因为 Get-Member调用会产生相同的输出[1]。 (不产生输出是 Compare-Object 表示未检测到差异的方式。)

    [1] Get-Member输出 Microsoft.PowerShell.Commands.MemberDefinition 的数组实例,一个用于输入对象类型的每个成员。在没有 -Property 的情况下参数传递给 Compare-Object ,这些实例作为一个整体进行比较,通过它们的.ToString()值,它产生每个成员的有意义的表示。

    关于powershell - char[] 和 char 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62615932/

    相关文章:

    powershell - 在 Powershell 中对路径列表进行排序

    powershell - 我们是否需要 Write-Output?

    powershell - 从 PowerShell 执行外部命令不接受参数

    powershell - 使用PowerShell的WinSCP会将目录添加到我要上传到的SFTP站点

    Powershell:未填充参数/参数

    powershell - 在Powershell中运行Hive时出现错误: “Please connect to a valid Azure HDInsight cluster before calling this cmdlet”

    powershell - 在 Powershell 中使用可变空白字符分割字符串

    powershell - Import-Csv - 成员已存在问题

    powershell - 如何在powershell中使用字符串列表过滤列表

    powershell - 可以在 PowerShell 中使用条件管道吗?