Skip to content

Instantly share code, notes, and snippets.

@GuyHarwood
Last active December 11, 2015 22:49
Show Gist options
  • Save GuyHarwood/4672426 to your computer and use it in GitHub Desktop.
Save GuyHarwood/4672426 to your computer and use it in GitHub Desktop.
Convert an int into an array of its respective enum flags
//the flags
[Flags]
public enum Role
{
None = 0x0,
User = 0x1,
Administrator = 0x2,
SysAdmin = 0x4
}
//lets say im a user and administrator
var int myRolesValue = 3;
var allRoles = Enum.GetValues(typeof(Role));
var myRoles = new List<Role>();
foreach (var role in allRoles)
{
if(((int)role | myRolesValue) == myRolesValue)
myRoles.Add((Role)role);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment