我可以帮您翻译成中文:在哪里可以找到Powershell .NET类型加速器列表?

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

2
这些并不严格意义上的别名,而是隐式命名空间。不过问得好。 - Scott Weinstein
3
我用 "类型加速器" 替换了您使用的 "别名"。在 PowerShell 中,别名是完全不同的东西;它们是命令、函数和脚本的快捷方式。 - Steven Murawski
1
@Steven:可能是个不错的选择;但在我看来,“类型名称别名”也是一个合适的术语。 - Noldorin
5个回答

13

自从四年前提出并回答这个问题以来,PowerShell 已经继续发展。@KeithHill 简明扼要的答案不幸地已经不再适用。我稍微挖掘了一下,发现必要的类只是有点 less exposed。好消息是,现在可以使用这行代码显示类型加速器列表...

[psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")::Get

Connect post中归功于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 Community Extensions (PSCX) 版本3.1.0,您现在可以使用类型加速器来列出所有类型加速器并只需调用此命令:

[accelerators]::get

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
...

请注意,您需要将新的“accelerators”加速器添加到字典中,因为TypeAccelerators类型不是公共的。使用.NET Reflector和大量空闲时间,你可以做出令人惊讶的事情。:-) Oisin,你太棒了!

这篇文章比Oisin早期的文章好多了(那篇文章有点像是一个hack)。 - JasonMArcher
2
我之前写了一个模块来列出、添加和删除自定义类型加速器……我刚刚在PoshCode上发布了一个更新:http://poshcode.org/1398 - Jaykul

8
请参见这篇博客文章中名为Type Name Aliases的部分。我相信这是别名的完整列表。
PowerShell 类型别名   对应的 .NET 类型
[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[]

[wmi] 似乎是列表中缺失的一个。 - Tyson Gilberstad
@Tyson:是的,我怀疑可能会有一两个缺失。但希望这能让你开始。如果你发现自己需要别名,只需搜索特定的别名即可。 :) - Noldorin
5
这里缺少很多(包括所有2.0版的),而且这里大部分的也并非真正的加速器。可以按照Keith Hill的帖子中的方法获取一个列表(可选修改)。 任何在“System”命名空间中的类型看起来都像是加速器,但这只是因为你总是可以省略类型名称前面的“System.”。 数组符号也不是单独的加速器,因为数组符号对于任何类型都是自动可用的,包括类型加速器。 - Jaykul

3
Noldorin提供了一些类型加速器的好列表。
此外,PowerShell还允许您使用类型文字来转换对象、调用静态方法、访问静态属性、反映以及任何其他您可能使用System.Type对象实例进行的操作。
要使用类型文字,只需将类的完整名称(命名空间和类名)(用点分隔命名空间和类名)括在方括号中,例如:
[System.Net.NetworkInformation.IPStatus]

PowerShell在尝试解析名称时也会提供一个前导"System.",因此如果您正在使用System*命名空间中的内容,则不需要显式使用。

[Net.NetworkInformation.IPStatus]

Oisin Grehan(一位PowerShell MVP)也在他的博客中发布了有关创建自己的类型加速器的文章


请查看Oisin在与Keith Hill的回答中所链接的关于该主题的最新帖子。 - JasonMArcher

1

这里是更完整的列表:

Key                   Value
---                   -----
adsi                  System.DirectoryServices.DirectoryEntry
adsisearcher          System.DirectoryServices.DirectorySearcher
array                 System.Array
bigint                System.Numerics.BigInteger
bool                  System.Boolean
byte                  System.Byte
char                  System.Char
cimclass              Microsoft.Management.Infrastructure.CimClass
cimconverter          Microsoft.Management.Infrastructure.CimConverter
ciminstance           Microsoft.Management.Infrastructure.CimInstance
cimtype               Microsoft.Management.Infrastructure.CimType
cultureinfo           System.Globalization.CultureInfo
datetime              System.DateTime
decimal               System.Decimal
double                System.Double
float                 System.Single
guid                  System.Guid
hashtable             System.Collections.Hashtable
initialsessionstate   System.Management.Automation.Runspaces.InitialSessionState
int                   System.Int32
int16                 System.Int16
int32                 System.Int32
int64                 System.Int64
ipaddress             System.Net.IPAddress
long                  System.Int64
mailaddress           System.Net.Mail.MailAddress
powershell            System.Management.Automation.PowerShell
psaliasproperty       System.Management.Automation.PSAliasProperty
pscredential          System.Management.Automation.PSCredential
pscustomobject        System.Management.Automation.PSObject
pslistmodifier        System.Management.Automation.PSListModifier
psmoduleinfo          System.Management.Automation.PSModuleInfo
psnoteproperty        System.Management.Automation.PSNoteProperty
psobject              System.Management.Automation.PSObject
psprimitivedictionary System.Management.Automation.PSPrimitiveDictionary
psscriptmethod        System.Management.Automation.PSScriptMethod
psscriptproperty      System.Management.Automation.PSScriptProperty
psvariable            System.Management.Automation.PSVariable
psvariableproperty    System.Management.Automation.PSVariableProperty
ref                   System.Management.Automation.PSReference
regex                 System.Text.RegularExpressions.Regex
runspace              System.Management.Automation.Runspaces.Runspace
runspacefactory       System.Management.Automation.Runspaces.RunspaceFactory
sbyte                 System.SByte
scriptblock           System.Management.Automation.ScriptBlock
securestring          System.Security.SecureString
single                System.Single
string                System.String
switch                System.Management.Automation.SwitchParameter
timespan              System.TimeSpan
type                  System.Type
uint16                System.UInt16
uint32                System.UInt32
uint64                System.UInt64
uri                   System.Uri
version               System.Version
void                  System.Void
wmi                   System.Management.ManagementObject
wmiclass              System.Management.ManagementClass
wmisearcher           System.Management.ManagementObjectSearcher
xml                   System.Xml.XmlDocument

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接