//FROM: https://stackoverflow.com/a/22222260 static EnumInfoContainer() { var flag = 1L; foreach (var value in Enum.GetValues(typeof(T)).Cast <T>()) { var bits = CastTo <long> .Cast(value); while (flag < bits) { flag <<= 1; } if (flag == bits) { Flags.Add(value); } } }
/// <summary> /// Performs bitwise OR on the specified values and returns the result. /// This method assumes <typeparamref name="T"/> is convertible to int64. /// </summary> public static T ToBitwise <T>(this IEnumerable <T> values) where T : Enum => values == null ? default : CastTo <T> .Cast(values.Select(CastTo <long> .Cast).Aggregate(0L, (a, b) => a | b));