예제 #1
0
        /// <summary>
        /// Constructs an object to describe the provided enumeration type.
        /// </summary>
        /// <param name="type">The enumeration type to describe.</param>
        protected EnumArgumentType(Type type) : base(type)
        {
            if (!type.GetTypeInfo().IsEnum)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            UnderlyingType = Enum.GetUnderlyingType(type);
            if (UnderlyingType == null)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            if (!ArgumentType.TryGetType(UnderlyingType, out IArgumentType underlyingArgType))
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            UnderlyingIntegerType = underlyingArgType as IntegerArgumentType;
            if (UnderlyingIntegerType == null)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            _values = GetAllValues(type).ToList();
            _valuesByCaseSensitiveName   = ConstructValueNameMap(_values, true);
            _valuesByCaseInsensitiveName = ConstructValueNameMap(_values, false);
            _valuesByValue = _values.ToDictionary(v => v.Value, v => v);
        }
예제 #2
0
        /// <summary>
        /// Primary constructor.
        /// </summary>
        /// <param name="type">The flag-based enumeration type to describe.
        /// </param>
        public FlagsEnumArgumentType(Type type) : base(type)
        {
            if (type.GetTypeInfo().GetSingleAttribute <FlagsAttribute>() == null)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            _underlyingType = Enum.GetUnderlyingType(type);
            if (!ArgumentType.TryGetType(_underlyingType, out IArgumentType underlyingArgType))
            {
                throw new NotSupportedException(nameof(type));
            }

            _underlyingIntegerType = (IntegerArgumentType)underlyingArgType;
        }