The problem is that you cannot write generic with "where T: System.Enum" or "where T: System.ValueType" .
Fortunately I've found a workaround. It works even better than i expected it would.
The bonus is an inderect call of .AllOf<T>(...). On the picture we see a call of non-generic AllOf( ...) which is forbidden because it's private method. And still there is no error . But hint clarifies the miracle. Visual studio converts behind the scene a call AllOf( ...) into AllOf<T>(...) which is really nice :).
- /// <summary>
- /// Checks wheither at least one of optional flags is set
- /// </summary>
- /// <param name="flagsWeHave">The flags we have.</param>
- /// <param name="flagsWeSearch">The flags we search for.</param>
- /// <returns> true, if at least one of the flags in bit representation of both values is matching </returns>
- private static bool AnyOf(this IConvertible flagsWeHave, IConvertible flagsWeSearch)
- {
- return (flagsWeHave.ToInt32(CultureInfo.InvariantCulture)
- & flagsWeSearch.ToInt32(CultureInfo.InvariantCulture)) > 0;
- }
- /// <summary>
- /// Checks wheither at least one of optional flags is set
- /// </summary>
- /// <param name="flagsWeHave">The flags we have.</param>
- /// <param name="flagsWeSearch">The flags we search for.</param>
- /// <returns> true, if at least one of the flags in bit representation of both values is matching </returns>
- public static bool AnyOf<T>(this T flagsWeHave, T flagsWeSearch) where T : IConvertible
- {
- if (!typeof(T).IsEnum && typeof(T) != typeof(int))
- throw new ArgumentOutOfRangeException("T can only be enum or int");
- return AnyOf(flagsWeHave as IConvertible, flagsWeSearch as IConvertible);
- }
- /// <summary>
- /// Checks wheither at least one of optional flags is set
- /// </summary>
- /// <param name="flagsWeHave">The flags we have.</param>
- /// <param name="flagsWeSearch">The flags we search for.</param>
- /// <returns> true, if at least one of the flags in bit representation of both values is matching </returns>
- private static bool AnyOf(this Enum flagsWeHave, Enum flagsWeSearch)
- {
- return AnyOf(flagsWeSearch as IConvertible, flagsWeSearch as IConvertible);
- }