Created
July 29, 2020 03:23
-
-
Save fiddyschmitt/605cc2338147b9116a099630d94be66b to your computer and use it in GitHub Desktop.
Enum extensions
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
public static IEnumerable<T> MaskToList<T>(this Enum mask) | |
{ | |
if (typeof(T).IsSubclassOf(typeof(Enum)) == false) | |
throw new ArgumentException(); | |
return Enum.GetValues(typeof(T)) | |
.Cast<Enum>() | |
.Where(m => mask.HasFlag(m)) | |
.Cast<T>(); | |
} | |
public static string MaskToString<T>(this Enum mask, string separator) | |
{ | |
string result = mask | |
.MaskToList<T>() | |
.Select(i => i.ToString()) | |
.ToString(separator); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment