Last active
December 11, 2015 22:49
-
-
Save GuyHarwood/4672426 to your computer and use it in GitHub Desktop.
Convert an int into an array of its respective enum flags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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