예제 #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);
        }
예제 #3
0
        // Token: 0x06000F4C RID: 3916 RVA: 0x0005AA98 File Offset: 0x00058C98
        private static EnumInfo InitializeValuesAndNames([Nullable(new byte[]
        {
            0,
            1,
            2
        })] StructMultiKey <Type, NamingStrategy> key)
        {
            Type value = key.Value1;

            string[] names  = Enum.GetNames(value);
            string[] array  = new string[names.Length];
            ulong[]  array2 = new ulong[names.Length];
            for (int i = 0; i < names.Length; i++)
            {
                string    text  = names[i];
                FieldInfo field = value.GetField(text, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                array2[i] = EnumUtils.ToUInt64(field.GetValue(null));
                string text2 = (from EnumMemberAttribute a in field.GetCustomAttributes(typeof(EnumMemberAttribute), true)
                                select a.Value).SingleOrDefault <string>();
                bool   hasSpecifiedName = text2 != null;
                string text3            = text2 ?? text;
                if (Array.IndexOf <string>(array, text3, 0, i) != -1)
                {
                    throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, text3, value.Name));
                }
                array[i] = ((key.Value2 != null) ? key.Value2.GetPropertyName(text3, hasSpecifiedName) : text3);
            }
            return(new EnumInfo(value.IsDefined(typeof(FlagsAttribute), false), array2, names, array));
        }
예제 #4
0
        public static bool TryToString(Type enumType, object value, NamingStrategy namingStrategy, out string name)
        {
            EnumInfo enumInfo = EnumUtils.ValuesAndNamesPerEnum.Get(new StructMultiKey <Type, NamingStrategy>(enumType, namingStrategy));
            ulong    num      = EnumUtils.ToUInt64(value);

            if (enumInfo.IsFlags)
            {
                name = EnumUtils.InternalFlagsFormat(enumInfo, num);
                return(name != null);
            }
            int num1 = Array.BinarySearch <ulong>(enumInfo.Values, num);

            if (num1 < 0)
            {
                name = null;
                return(false);
            }
            name = enumInfo.ResolvedNames[num1];
            return(true);
        }
예제 #5
0
        private static EnumInfo InitializeValuesAndNames(StructMultiKey <Type, NamingStrategy> key)
        {
            Type value1 = key.Value1;

            string[] names     = Enum.GetNames(value1);
            string[] strArrays = new string[(int)names.Length];
            ulong[]  num       = new ulong[(int)names.Length];
            for (int i = 0; i < (int)names.Length; i++)
            {
                FieldInfo field = value1.GetField(names[i], BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                num[i] = EnumUtils.ToUInt64(field.GetValue(null));
                string str = (
                    from EnumMemberAttribute a in field.GetCustomAttributes(typeof(EnumMemberAttribute), true)
                    select a.Value).SingleOrDefault <string>() ?? field.Name;
                if (Array.IndexOf <string>(strArrays, str, 0, i) != -1)
                {
                    throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, str, value1.Name));
                }
                strArrays[i] = (key.Value2 != null ? key.Value2.GetPropertyName(str, false) : str);
            }
            return(new EnumInfo(value1.IsDefined(typeof(FlagsAttribute), false), num, names, strArrays));
        }