Exemple #1
0
        public CommandGroupArgumentType(Type type) : base(type)
        {
            if (!type.GetTypeInfo().IsGenericType)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            var typeParams = type.GetTypeInfo().GetGenericArguments();

            if (typeParams.Length != 1)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            if (!type.GetGenericTypeDefinition().IsEffectivelySameAs(typeof(CommandGroup <>)))
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            _commandTypeType = typeParams[0];
            if (!_commandTypeType.GetTypeInfo().IsEnum)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

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

            _commandArgType = (IEnumArgumentType)commandArgType;
        }
        /// <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);
        }
Exemple #3
0
 /// <summary>
 /// Constructor for use by derived classes.
 /// </summary>
 /// <param name="type">Type described by this object.</param>
 /// <param name="elementType">Type of elements stored within the
 /// collection type described by this object.</param>
 protected CollectionArgumentTypeBase(Type type, Type elementType)
     : base(type)
 {
     if (!ArgumentType.TryGetType(elementType, out _elementArgumentType))
     {
         throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Strings.ElementTypeNotSupported, type));
     }
 }
        /// <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;
        }
Exemple #5
0
        /// <summary>
        /// Constructs an object to describe the provided KeyValuePair type.
        /// </summary>
        /// <param name="type">The KeyValuePair type to describe.</param>
        public KeyValuePairArgumentType(Type type)
            : base(type)
        {
            if (!type.GetTypeInfo().IsGenericType)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            if (!type.GetGenericTypeDefinition().IsEffectivelySameAs(typeof(KeyValuePair <,>)))
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            var typeParams = type.GetTypeInfo().GetGenericArguments();

            if (!ArgumentType.TryGetType(typeParams[0], out _keyType) ||
                !ArgumentType.TryGetType(typeParams[1], out _valueType))
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Strings.ConstituentTypeNotSupported, type.Name));
            }

            _constructor = type.GetTypeInfo().GetConstructor(typeParams);
            Debug.Assert(_constructor != null);
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="type">The primitive object type to find a corresponding
 /// IArgumentType implementation for (e.g. System.String).</param>
 /// <param name="parser">Optionally provides an override implementation
 /// of the base argument type's string parser implementation.</param>
 /// <param name="formatter">Optionally provides an override
 /// implementation of the base argument type's object formatter
 /// implementation.</param>
 /// <param name="completer">Optionally provides an override
 /// implementation of the base argument type's string completer
 /// implementation.</param>
 public ArgumentTypeExtension(Type type, IStringParser parser = null, IObjectFormatter formatter = null, IStringCompleter completer = null) : this(ArgumentType.GetType(type), parser, formatter, completer)
 {
 }