Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fiddyschmitt/605cc2338147b9116a099630d94be66b to your computer and use it in GitHub Desktop.
Save fiddyschmitt/605cc2338147b9116a099630d94be66b to your computer and use it in GitHub Desktop.
Enum extensions
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