private static RequireIntEnumsAndBitSet <TEnum, TFlag> CreateDefault() { RequireIntEnumsAndBitSet <TEnum, TFlag> result = new RequireIntEnumsAndBitSet <TEnum, TFlag>(); result.BitSet = (BitSetAttribute)typeof(TFlag).GetCustomAttributes(typeof(BitSetAttribute), false).SingleOrDefault(); result.IsValid = typeof(TEnum).IsEnum && //Marshal.SizeOf(Enum.GetUnderlyingType(typeof(TEnum))) <= sizeof(int) && // check needed? result.BitSet != null && result.BitSet.SetEnumType == typeof(TEnum); return(result); }
public static bool InSet <TEnum, TFlag>(this TEnum bit, TFlag set, RequireIntEnumsAndBitSet <TEnum, TFlag> tag = default(RequireIntEnumsAndBitSet <TEnum, TFlag>)) where TEnum : struct, IConvertible, IComparable where TFlag : struct, IConvertible { if (!RequireIntEnumsAndBitSet <TEnum, TFlag> .Default.IsValid) { throw new NotSupportedException(); } if (!RequireIntEnumsAndBitSet <TEnum, TFlag> .Default.BitSet.IsInrange(bit)) { throw new ArgumentOutOfRangeException("bit"); } ulong bitvalue = 1UL << bit.ToInt32(null); ulong flagsset = set.ToUInt64(null); return((flagsset & bitvalue) != 0UL); }