예제 #1
0
        public static IList <T> GetFlagsValues <T>(T value)
            where T : struct
        {
            Type type = typeof(T);

            if (!type.IsDefined(typeof(FlagsAttribute), false))
            {
                throw new ArgumentException("Enum type {0} is not a set of flags.".FormatWith(CultureInfo.InvariantCulture, type));
            }
            Type      underlyingType     = Enum.GetUnderlyingType(value.GetType());
            ulong     num                = EnumUtils.ToUInt64(value);
            EnumInfo  enumValuesAndNames = EnumUtils.GetEnumValuesAndNames(type);
            IList <T> ts = new List <T>();

            for (int i = 0; i < (int)enumValuesAndNames.Values.Length; i++)
            {
                ulong values = enumValuesAndNames.Values[i];
                if ((num & values) == values && values != 0)
                {
                    ts.Add((T)Convert.ChangeType(values, underlyingType, CultureInfo.CurrentCulture));
                }
            }
            if (ts.Count == 0)
            {
                if (((IEnumerable <ulong>)enumValuesAndNames.Values).Any <ulong>((ulong v) => v == 0L))
                {
                    ts.Add(default(T));
                }
            }
            return(ts);
        }
예제 #2
0
        public static IList <T> GetFlagsValues <T>(T value) where T : struct
        {
            Type typeFromHandle = typeof(T);

            if (!typeFromHandle.IsDefined(typeof(FlagsAttribute), false))
            {
                throw new ArgumentException("Enum type {0} is not a set of flags.".FormatWith(CultureInfo.InvariantCulture, typeFromHandle));
            }
            Type      underlyingType     = Enum.GetUnderlyingType(value.GetType());
            ulong     num                = EnumUtils.ToUInt64(value);
            EnumInfo  enumValuesAndNames = EnumUtils.GetEnumValuesAndNames(typeFromHandle);
            IList <T> list               = new List <T>();

            for (int i = 0; i < enumValuesAndNames.Values.Length; i++)
            {
                ulong num2 = enumValuesAndNames.Values[i];
                if ((num & num2) == num2 && num2 != 0UL)
                {
                    list.Add((T)((object)Convert.ChangeType(num2, underlyingType, CultureInfo.CurrentCulture)));
                }
            }
            if (list.Count == 0)
            {
                if (enumValuesAndNames.Values.Any((ulong v) => v == 0UL))
                {
                    list.Add(default(T));
                }
            }
            return(list);
        }