コード例 #1
0
        public int AddEnum(Type type, AbstractTypeSafeEnum instance)
        {
            if (!_enums.ContainsKey(type))
            {
                _enums.Add(type, new Dictionary <string, AbstractTypeSafeEnum>());
            }

            Dictionary <string, AbstractTypeSafeEnum> enumValues = _enums[type];

            enumValues.Add(instance.Value.ToUpperInvariant(), instance);

            if (AbstractTypeSafeEnum.SupportsMultipleValues(type))
            {
                if (enumValues.Count < _possibleFlagValues.Length)
                {
                    return(_possibleFlagValues[enumValues.Count - 1]);
                }
                else
                {
                    throw new InvalidOperationException("No more than 30 enum values are supported.");
                }
            }
            else
            {
                return(-1);
            }
        }
コード例 #2
0
        public T GetEnum <T>(string[] s) where T : AbstractTypeSafeEnum
        {
            Type type = typeof(T);

            if (!_enums.ContainsKey(type))
            {
                return(null);
            }

            if (s.Length == 1)
            {
                string value = s[0].ToUpperInvariant();
                if (_enums[type].ContainsKey(value))
                {
                    return((T)_enums[type][value]);
                }
            }
            else
            {
                if (!AbstractTypeSafeEnum.SupportsMultipleValues(type))
                {
                    //    throw new ArgumentException(Resources.EnumDoesNotSupportMultipleValues);
                }

                List <T> values = new List <T>();

                foreach (string v in s)
                {
                    string value = v.ToUpperInvariant();

                    if (_enums[type].ContainsKey(value))
                    {
                        values.Add((T)_enums[type][value]);
                    }
                }

                return(AbstractTypeSafeEnum.CombineValues <T>(values.ToArray()));
            }

            return(null);
        }