feat: Added GetAllFlagCombinationsExceptNone method to EnumUtils

This commit is contained in:
Jack Kanarish
2018-04-03 12:28:58 -04:00
parent 44a87c9907
commit 62e107b520

View File

@@ -305,6 +305,25 @@ namespace ICD.Common.Utils
return GetFlags(value).Except(none);
}
/// <summary>
/// Gets all of the flag combinations on the given flag enum value.
///
/// IE: If you have an enum type with flags{a, b, c}, and you pass this method {a|b},
/// It will return {a, b, a|b}
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static IEnumerable<T> GetAllFlagCombinationsExceptNone<T>(T value)
{
if (!IsEnum(value))
// ReSharper disable once CompareNonConstrainedGenericWithNull
throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
int maxEnumValue = (GetValues<T>().Max(v => (int)(object)v) * 2) -1 ;
return Enumerable.Range(1, maxEnumValue).Cast<T>().Where(v => HasFlags(value, v));
}
/// <summary>
/// Gets an enum value of the given type with every flag set.
/// </summary>