【发布时间】:2012-04-20 00:27:42
【问题描述】:
在 PowerShell 中,您可以使用方括号指定类型,如下所示:
PS C:\Users\zippy> [int]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
还有像 [xml] 这样的内置类型加速器,当您希望将某些内容投射到 XmlDocument 时,它可以节省一些击键。
PS C:\Users\zippy> [xml]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False XmlDocument System.Xml.XmlNode
您可以通过以下两个命令之一生成列表:
- PS v2.0
[type]::gettype("System.Management.Automation.TypeAccelerators")::Get - PS v3.0
[psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")::Get
PowerShell 3.0 添加了一个名为 [ordered] 的运算符。虽然它不是类型别名。
PS C:\Users\zippy> [ordered]
Unable to find type [ordered]: make sure that the assembly containing this type is loaded.
At line:1 char:1
+ [ordered]
+ ~~~~~~~~~
+ CategoryInfo : InvalidOperation: (ordered:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
但是,它可以将Hashtables 转换为OrderedDictionarys。
PS C:\Users\zippy> ([ordered]@{}).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True OrderedDictionary System.Object
所以我的问题是,如果 [ordered] 不是类型加速器,它是什么?
【问题讨论】:
标签: powershell-3.0