예제 #1
0
        public static object ParseEnumName(string enumText, bool isNullable, bool disallowValue, Type t)
        {
            if (enumText == string.Empty && isNullable)
            {
                return(null);
            }

            string finalEnumText;

            BidirectionalDictionary <string, string> map = EnumMemberNamesPerType.Get(t);
            string resolvedEnumName;

            if (TryResolvedEnumName(map, enumText, out resolvedEnumName))
            {
                finalEnumText = resolvedEnumName;
            }
            else if (enumText.IndexOf(',') != -1)
            {
                string[] names = enumText.Split(',');
                for (int i = 0; i < names.Length; i++)
                {
                    string name = names[i].Trim();

                    names[i] = TryResolvedEnumName(map, name, out resolvedEnumName)
                        ? resolvedEnumName
                        : name;
                }

                finalEnumText = string.Join(", ", names);
            }
            else
            {
                finalEnumText = enumText;

                if (disallowValue)
                {
                    bool isNumber = int.TryParse(finalEnumText, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out int _);

                    if (isNumber)
                    {
                        throw new FormatException("Integer string '{0}' is not allowed.".FormatWith(CultureInfo.InvariantCulture, enumText));
                    }
                }
            }

            return(Enum.Parse(t, finalEnumText, true));
        }
예제 #2
0
        private static BidirectionalDictionary <string, string> InitializeEnumType(
            Type type)
        {
            BidirectionalDictionary <string, string> bidirectionalDictionary = new BidirectionalDictionary <string, string>((IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase, (IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase);

            foreach (FieldInfo field in type.GetFields())
            {
                string name   = field.Name;
                string second = field.GetCustomAttributes(typeof(EnumMemberAttribute), true).Cast <EnumMemberAttribute>().Select <EnumMemberAttribute, string>((Func <EnumMemberAttribute, string>)(a => a.Value)).SingleOrDefault <string>() ?? field.Name;
                if (bidirectionalDictionary.TryGetBySecond(second, out string _))
                {
                    throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)second, (object)type.Name));
                }
                bidirectionalDictionary.Set(name, second);
            }
            return(bidirectionalDictionary);
        }
예제 #3
0
        public static string ToEnumName(Type enumType, string enumText, bool camelCaseText)
        {
            BidirectionalDictionary <string, string> bidirectionalDictionary = EnumUtils.EnumMemberNamesPerType.Get(enumType);

            string[] strArray = enumText.Split(',');
            for (int index = 0; index < strArray.Length; ++index)
            {
                string first = strArray[index].Trim();
                string second;
                bidirectionalDictionary.TryGetByFirst(first, out second);
                second = second ?? first;
                if (camelCaseText)
                {
                    second = StringUtils.ToCamelCase(second);
                }
                strArray[index] = second;
            }
            return(string.Join(", ", strArray));
        }
예제 #4
0
        public static string ToEnumName(Type enumType, string enumText, bool camelCaseText)
        {
            string camelCase;
            BidirectionalDictionary <string, string> bidirectionalDictionary = EnumUtils.EnumMemberNamesPerType.Get(enumType);

            string[] strArrays = enumText.Split(new char[] { ',' });
            for (int i = 0; i < (int)strArrays.Length; i++)
            {
                string str = strArrays[i].Trim();
                bidirectionalDictionary.TryGetByFirst(str, out camelCase);
                camelCase = camelCase ?? str;
                if (camelCaseText)
                {
                    camelCase = StringUtils.ToCamelCase(camelCase);
                }
                strArrays[i] = camelCase;
            }
            return(string.Join(", ", strArrays));
        }
예제 #5
0
        private static BidirectionalDictionary <string, string> InitializeEnumType(Type type)
        {
            string str;
            BidirectionalDictionary <string, string> bidirectionalDictionary = new BidirectionalDictionary <string, string>(StringComparer.OrdinalIgnoreCase, StringComparer.OrdinalIgnoreCase);

            FieldInfo[] fields = type.GetFields();
            for (int i = 0; i < (int)fields.Length; i++)
            {
                FieldInfo fieldInfo = fields[i];
                string    name      = fieldInfo.Name;
                string    str1      = (
                    from EnumMemberAttribute a in fieldInfo.GetCustomAttributes(typeof(EnumMemberAttribute), true)
                    select a.Value).SingleOrDefault <string>() ?? fieldInfo.Name;
                if (bidirectionalDictionary.TryGetBySecond(str1, out str))
                {
                    throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, str1, type.Name));
                }
                bidirectionalDictionary.Set(name, str1);
            }
            return(bidirectionalDictionary);
        }
예제 #6
0
        public static object ParseEnumName(string enumText, bool isNullable, Type t)
        {
            if (enumText == string.Empty && isNullable)
            {
                return(null);
            }

            string finalEnumText;

            BidirectionalDictionary <string, string> map = EnumMemberNamesPerType.Get(t);
            string resolvedEnumName;

            if (TryResolvedEnumName(map, enumText, out resolvedEnumName))
            {
                finalEnumText = resolvedEnumName;
            }
            else if (enumText.IndexOf(',') != -1)
            {
                string[] names = enumText.Split(',');
                for (int i = 0; i < names.Length; i++)
                {
                    string name = names[i].Trim();

                    names[i] = TryResolvedEnumName(map, name, out resolvedEnumName)
                        ? resolvedEnumName
                        : name;
                }

                finalEnumText = string.Join(", ", names);
            }
            else
            {
                finalEnumText = enumText;
            }

            return(Enum.Parse(t, finalEnumText, true));
        }
예제 #7
0
        public static string ToEnumName(Type enumType, string enumText, bool camelCaseText)
        {
            BidirectionalDictionary <string, string> map = EnumMemberNamesPerType.Get(enumType);

            string[] names = enumText.Split(',');
            for (int i = 0; i < names.Length; i++)
            {
                string name = names[i].Trim();

                map.TryGetByFirst(name, out string resolvedEnumName);
                resolvedEnumName = resolvedEnumName ?? name;

                if (camelCaseText)
                {
                    resolvedEnumName = StringUtils.ToCamelCase(resolvedEnumName);
                }

                names[i] = resolvedEnumName;
            }

            string finalName = string.Join(", ", names);

            return(finalName);
        }
예제 #8
0
        private static BidirectionalDictionary <string, string> InitializeEnumType(Type type)
        {
            BidirectionalDictionary <string, string> map = new BidirectionalDictionary <string, string>(
                StringComparer.OrdinalIgnoreCase,
                StringComparer.OrdinalIgnoreCase);

            foreach (FieldInfo f in type.GetFields())
            {
                string n1 = f.Name;
                string n2;

                n2 = f.Name;

                string s;
                if (map.TryGetBySecond(n2, out s))
                {
                    throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, n2, type.Name));
                }

                map.Set(n1, n2);
            }

            return(map);
        }