鉴于我目前enum
:
Add-Type -TypeDefinition @"
[System.Flags]
public enum FlagsEnum {
None = 0,
SummaryInfo = 1,
ReportOptions = 2,
ParameterFields = 4
}
"@
有没有办法创建一个将所有位设置为 1 的条目?此语法会导致错误:
Add-Type -TypeDefinition @"
[System.Flags]
public enum FlagsEnum {
None = 0,
SummaryInfo = 1,
ReportOptions = 2,
ParameterFields = 4,
All = (SummaryInfo -bor ReportOptions -bor ParameterFields)
}
"@
** 编辑 **
更改了声明:
Add-Type -TypeDefinition @"
[System.Flags]
public enum FlagsEnum {
None = 0,
SummaryInfo = 1,
ReportOptions = 2,
ParameterFields = 4,
All = (SummaryInfo | ReportOptions | ParameterFields)
}
"@
编码:
$flags = [FlagsEnum]::All
if ( $flags -band [FlagsEnum]::SummaryInfo ) { write-host "add SummaryInfo" }
if ( $flags -band [FlagsEnum]::ReportOptions ) { write-host "add ReportOptions" }
if ( $flags -band [FlagsEnum]::ParameterFields ) { write-host "add ParameterFields" }
结果:
Add-Type : Cannot add type. The type name 'FlagsEnum' already exists.
At C:\Documents and Settings\xxx\My Documents\WindowsPowerShell\Scripts\enums.ps1:3 char:9
+ Add-Type <<<< -TypeDefinition @"
+ CategoryInfo : InvalidOperation: (FlagsEnum:String) [Add-Type], Exception
+ FullyQualifiedErrorId : TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand