我在哪里可以找到Powershell .NET类型加速器列表?

Tys*_*tad 17 .net powershell types accelerator

在PowerShell中,您可以使用[xml]来表示[System.Xml.XmlDocument].你知道在哪里可以找到这些类型加速器的列表吗?

这些加速器是否特定于PowerShell或.NET?

Mic*_*ens 13

自从四年前提出并回答了这个问题以来,PowerShell一直在不断发展.不幸的是,@ KeithHill的简明回答不再有效.我做了一点挖掘,发现必需的类只是暴露了一点.在光明的一面,现在可以只用这种显示类型加速器的名单一个代码行...

[psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")::Get
Run Code Online (Sandbox Code Playgroud)

...在这篇Connect帖子中归因于Jaykul .

这是一个部分输出:

Key                    Value
---                    -----
Alias                  System.Management.Automation.AliasAttribute
AllowEmptyCollection   System.Management.Automation.AllowEmptyCollectionAttribute
AllowEmptyString       System.Management.Automation.AllowEmptyStringAttribute
AllowNull              System.Management.Automation.AllowNullAttribute
array                  System.Array
bool                   System.Boolean
byte                   System.Byte
char                   System.Char
CmdletBinding          System.Management.Automation.CmdletBindingAttribute
datetime               System.DateTime
decimal                System.Decimal
adsi                   System.DirectoryServices.DirectoryEntry
adsisearcher           System.DirectoryServices.DirectorySearcher
double                 System.Double
float                  System.Single
single                 System.Single
guid                   System.Guid
hashtable              System.Collections.Hashtable
int                    System.Int32
. . .

2014.03.15更新

PowerShell社区扩展(PSCX)版本3.1.0开始,您现在可以使用类型加速器列出所有类型加速器并只调用它:

[accelerators]::get
Run Code Online (Sandbox Code Playgroud)


小智 12

明确的方法是在这篇优秀的博客文章中做Oisin贬低的事情:

PS> $acceleratorsType = [type]::gettype("System.Management.Automation.TypeAccelerators")
PS> $acceleratorsType

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
False    False    TypeAccelerators                         System.Object


PS> $acceleratorsType::Add("accelerators", $acceleratorsType)
PS> [accelerators]::Get

Key                                                         Value
---                                                         -----
int                                                         System.Int32
...
Run Code Online (Sandbox Code Playgroud)

请注意,您必须将新的"加速器"加速器添加到字典中,因为TypeAccelerators类型不是公共的.令人惊讶的是你可以使用.NET Reflector和大量的业余时间.:-)你摇滚Oisin!

  • 我写了一个模块来列出,添加和删除自定义类型加速器...我刚刚在PoshCode上发布了一个更新:http://poshcode.org/1398 (2认同)

Nol*_*rin 7

见标题为类型名称别名这个博客帖子.我相信这是一个完整的别名列表.

PowerShell Type Alias   Corresponding .NET Type
[int]                   System.Int32
[int[]]                 System.Int32[]
[long]                  System.Int64
[long[]]                System.Int64[]
[string]                System.String
[string[]]              System.String[]
[char]                  System.Char
[char[]]                System.Char[]
[bool]                  System.Boolean
[bool[]]                System.Boolean[]
[byte]                  System.Byte
[byte[]]                System.Byte[]
[double]                System.Double
[double[]]              System.Double[]
[decimal]               System.Decimal
[decimal[]]             System.Decimal[]
[float]                 System.Single
[single]                System.Single
[regex]                 System.Text.RegularExpression.Regex
[array]                 System.Array
[xml]                   System.Xml.XmlDocument
[scriptblock]           System.Management.Automation.ScriptBlock
[switch]                System.Management.Automation.SwitchParameter
[hashtable]             System.Collections.Hashtable
[psobject]              System.Management.Automation.PSObject
[type]                  System.Type
[type[]]                System.Type[]

  • 有很多缺失(包括所有2.0的),而且这里的大多数都不是技术上的加速器.使用Keith Hill的帖子中的方法获取列表(并可选择修改它)."System"命名空间中的任何类型可能看起来像加速器,但这只是因为您可以*始终*离开"系统".离开类型的前面...数组符号也不是单独的加速器,因为数组符号可自动用于任何类型,包括类型加速器. (5认同)