Skip to content

Instantly share code, notes, and snippets.

@dlwyatt
Last active September 26, 2016 18:38
Show Gist options
  • Save dlwyatt/36c603dfc9523ecd1ee0 to your computer and use it in GitHub Desktop.
Save dlwyatt/36c603dfc9523ecd1ee0 to your computer and use it in GitHub Desktop.
Flags Enum without explicit values
# Add an enum with All constant
Add-Type -TypeDefinition @'
using System;
[Flags]
public enum EnumSample
{
One,
Two,
Three,
Four,
Five,
All = One | Two | Three | Four | Five
}
'@
[EnumSample]'Four,Five' # All
[EnumSample]::Four.HasFlag([EnumSample]::Two) # True
[EnumSample]'Two,Three' # Four
# Displaying the problem. Flags enum are supposed to have each "flag" as a power of two, eg
# 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, etc. When you let the enum choose its own values,
# even with the [Flags] attribute applied, that doesn't happen.
foreach ($name in [Enum]::GetNames([EnumSample]))
{
[pscustomobject] @{
Name = $name
Value = [EnumSample]::$name.value__
Hex = '0x{0:X8}' -f [EnumSample]::$name.value__
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment