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

0s7z1bwu  于 2023-04-22  发布在  .NET
关注(0)|答案(5)|浏览(124)

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

yrefmtwq

yrefmtwq1#

自从这个问题在四年前被提出并回答以来,PowerShell一直在不断发展。@KeithHill的简洁答案不幸地不再起作用。我做了一点挖掘,发现必需的类只是暴露得少了一点。从好的方面来说,类型加速器的列表现在可能只显示这行代码...

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

.归因于Jaykul在这个Connect post.
以下是部分输出:

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
m1m5dgzv

m1m5dgzv2#

最后的方法是按照Oisin在excellent blog post中所演示的来做:

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和大量的空闲时间做很多令人惊讶的事情。

iaqfqrcu

iaqfqrcu3#

请参阅this blog post中标题为 * 类型名称别名 * 的部分。我相信这是别名的完整列表。

**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[]
bd1hkmkf

bd1hkmkf4#

@Noldorin有一个很好的列表,列出了一些类型加速器。
PowerShell还允许您使用类型文本来强制转换对象、调用静态方法、访问静态属性、反射以及您可能对System.Type对象的示例执行的任何其他操作。
为了使用类型字面量,您只需将类(或结构或枚举)的全名(命名空间和类名)(用句点分隔命名空间和类名)括在括号中,如:

[System.Net.NetworkInformation.IPStatus]

PowerShell还将在尝试解析名称时提供前导“System.”,因此如果您正在使用System* 命名空间中的内容,则不需要显式使用该名称。

[Net.NetworkInformation.IPStatus]

Oisin Grehan (a PowerShell MVP) also has a blog post about creating your own type accelerators

siv3szwd

siv3szwd5#

以下是一个更完整的列表:

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

相关问题