关于类型运算符
简短说明
介绍适用于 Microsoft .NET 类型的运算符。
详细说明
布尔类型运算符 (-is
和 -isNot
) 指示对象是否为指定 .NET 类型的实例。 如果类型匹配,运算符 -is
返回值 TRUE ,否则返回 FALSE 值。 如果类型匹配,运算符 -isNot
将返回 FALSE 值,否则返回 TRUE 值。
运算符 -as
尝试将输入对象转换为指定的 .NET 类型。 如果成功,则返回转换后的对象。 如果失败,则返回 $null
。 它不返回错误。
下表列出了 PowerShell 中的类型运算符。
运算符 | 说明 | 示例 |
---|---|---|
-is |
输入时返回 TRUE | (get-date) -is [DateTime] |
是 | True |
|
指定的 .NET 类型。 | ||
-isNot |
输入时返回 TRUE | (get-date) -isNot [DateTime] |
不是 的实例 | False |
|
specified.NET 类型。 | ||
-as |
将输入转换为 | "5/7/07" -as [DateTime] |
指定的 .NET 类型。 | Monday, May 7, 2007 12:00:00 AM |
类型运算符的语法如下所示:
<input> <operator> [.NET type]
还可以使用以下语法:
<input> <operator> ".NET type"
.NET 类型可以编写为括号中的类型名称或字符串,例如 [DateTime]
System.DateTime 的 或 "DateTime"
。 如果该类型不在系统命名空间的根目录中,请指定对象类型的全名。 可以省略“System.”。 例如,若要指定 System.Diagnostics.Process,请输入 [System.Diagnostics.Process]
、 [Diagnostics.Process]
或 "Diagnostics.Process"
。
类型运算符始终作为一个整体对输入对象进行操作。 也就是说,如果输入对象是集合,则测试的是 集合 类型,而不是 集合元素的类型。
-is/isNot 运算符
布尔类型运算符 (-is
,) -isNot
始终返回布尔值,即使输入是对象的集合也是如此。
如果 <input>
是与 .NET 类型相同的类型或 派生 自 .NET 类型,则 -is
运算符返回 $True
。
例如, DirectoryInfo 类型派生自 FileSystemInfo 类型。 因此,这两个示例都返回 True。
PS> (Get-Item /) -is [System.IO.DirectoryInfo]
True
PS> (Get-Item /) -is [System.IO.FileSystemInfo]
True
-is
如果 在比较中实现 接口, <input>
运算符也可以匹配接口。 在此示例中,输入是一个数组。 数组实现 System.Collections.IList 接口。
PS> 1, 2 -is [System.Collections.IList]
True
-as 运算符
运算符 -as
尝试将输入对象转换为指定的 .NET 类型。 如果成功,则返回转换后的对象。 如果失败,则返回 $null
。 它不返回错误。
<input>
如果 是派生自 .NET 类型-as
的类型,则传递将返回未更改的输入对象。 例如, DirectoryInfo 类型派生自 FileSystemInfo 类型。 因此,在以下示例中,对象类型保持不变:
PS> $fsroot = (Get-Item /) -as [System.IO.FileSystemInfo]
PS> $fsroot.GetType().FullName
System.IO.DirectoryInfo
转换 DateTime 类型区分区域性
与类型转换不同,使用 -as
运算符转换为 [DateTime]
类型仅适用于根据当前区域性规则设置格式的字符串。
PS> [cultureinfo]::CurrentCulture = 'fr-FR'
PS> '13/5/20' -as [datetime]
mercredi 13 mai 2020 00:00:00
PS> '05/13/20' -as [datetime]
PS> [datetime]'05/13/20'
mercredi 13 mai 2020 00:00:00
PS> [datetime]'13/05/20'
InvalidArgument: Cannot convert value "13/05/20" to type "System.DateTime".
Error: "String '13/05/20' was not recognized as a valid DateTime."
若要查找对象的 .NET 类型,请使用 Get-Member
cmdlet。 或者,将所有对象的 GetType 方法与此方法的 FullName 属性一起使用。 例如,以下语句获取命令的返回值 Get-Culture
的类型:
PS> (Get-Culture).GetType().FullName
System.Globalization.CultureInfo
示例
以下示例演示了 Type 运算符的一些用法:
PS> 32 -is [Float]
False
PS> 32 -is "int"
True
PS> (get-date) -is [DateTime]
True
PS> "12/31/2007" -is [DateTime]
False
PS> "12/31/2007" -is [String]
True
PS> (get-process PowerShell)[0] -is [System.Diagnostics.Process]
True
PS> (get-command get-member) -is [System.Management.Automation.CmdletInfo]
True
以下示例显示,当输入是 对象的集合时,匹配类型是集合的 .NET 类型,而不是集合中各个对象的类型。
在此示例中,尽管 和 Get-UICulture
cmdlet 都Get-Culture
返回 System.Globalization.CultureInfo 对象,但这些对象的集合是 System.Object 数组。
PS> (get-culture) -is [System.Globalization.CultureInfo]
True
PS> (get-uiculture) -is [System.Globalization.CultureInfo]
True
PS> (get-culture), (get-uiculture) -is [System.Globalization.CultureInfo]
False
PS> (get-culture), (get-uiculture) -is [Array]
True
PS> (get-culture), (get-uiculture) | foreach {
$_ -is [System.Globalization.CultureInfo])
}
True
True
PS> (get-culture), (get-uiculture) -is [Object]
True
以下示例演示如何使用 -as
运算符。
PS> "12/31/07" -is [DateTime]
False
PS> "12/31/07" -as [DateTime]
Monday, December 31, 2007 12:00:00 AM
PS> $date = "12/31/07" -as [DateTime]
C:\PS>$a -is [DateTime]
True
PS> 1031 -as [System.Globalization.CultureInfo]
LCID Name DisplayName
---- ---- -----------
1031 de-DE German (Germany)
以下示例显示, -as
当 运算符无法将输入对象转换为 .NET 类型时,它将返回 $null
。
PS> 1031 -as [System.Diagnostics.Process]
PS>