コード例 #1
0
        public object?Parse(IArgument argument, IEnumerable <string> values)
        {
            // TODO: when _type & values is IEnumerable but not ICollection
            //       DO NOT enumerate values here as it could be a stream.
            var listInstance = _type.IsArray
                ? new ArrayList()
                : values is ICollection <string> || _type.IsCollection()
                    ? CreateGenericList()
                    : null;

            if (listInstance == null)
            {
                if (_underlyingType == typeof(string))
                {
                    return(values);
                }

                // must create delegate of correct type to invoke command method
                // while casting only as the stream is consumed
                Func <IEnumerable, object> f = Enumerable.Cast <object>;
                var cast       = f.Method.GetGenericMethodDefinition().MakeGenericMethod(_underlyingType);
                var enumerable = values.Select(v => _argumentTypeDescriptor.ParseString(argument, v));
                return(cast.Invoke(null, new[] { enumerable }));
            }

            foreach (string stringValue in values)
            {
                listInstance.Add(_argumentTypeDescriptor.ParseString(argument, stringValue));
            }

            return(_type.IsArray
                ? ((ArrayList)listInstance).ToArray(_underlyingType)
                : listInstance);
        }
コード例 #2
0
 public object?ParseString(IArgument argument, string value)
 {
     try
     {
         return(_innerDescriptor.ParseString(argument, value));
     }
     catch (FormatException)
     {
         throw new ValueParsingException(
                   Resources.A.Error_Value_is_not_valid_for_type(value,
                                                                 _innerDescriptor.GetDisplayName(argument)));
     }
     catch (ArgumentException)
     {
         throw new ValueParsingException(
                   Resources.A.Error_Value_is_not_valid_for_type(value,
                                                                 _innerDescriptor.GetDisplayName(argument)));
     }
     catch (OperationCanceledException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw new ValueParsingException(
                   Resources.A.Error_Failed_parsing_value_for_type(value,
                                                                   _innerDescriptor.GetDisplayName(argument)), ex);
     }
 }
コード例 #3
0
        public object?Parse(IArgument argument, IEnumerable <string> values)
        {
            var value = values.SingleOrDefaultOrThrow(() => ThrowMultiForSingleEx(argument));

            return(value is null
                ? null
                : _argumentTypeDescriptor.ParseString(argument, value));
        }
コード例 #4
0
 public object ParseString(IArgument argument, string value)
 {
     try
     {
         return(_innerDescriptor.ParseString(argument, value));
     }
     catch (FormatException)
     {
         throw new ValueParsingException(
                   $"'{value}' is not a valid {_innerDescriptor.GetDisplayName(argument)}");
     }
     catch (ArgumentException)
     {
         throw new ValueParsingException(
                   $"'{value}' is not a valid {_innerDescriptor.GetDisplayName(argument)}");
     }
 }
コード例 #5
0
 public dynamic ParseString(ArgumentInfo argumentInfo, string value)
 {
     try
     {
         return(_argumentTypeDescriptor.ParseString(argumentInfo, value));
     }
     catch (FormatException)
     {
         throw new ValueParsingException(
                   $"'{value}' is not a valid {_argumentTypeDescriptor.GetDisplayName(argumentInfo)}");
     }
     catch (ArgumentException)
     {
         throw new ValueParsingException(
                   $"'{value}' is not a valid {_argumentTypeDescriptor.GetDisplayName(argumentInfo)}");
     }
 }
コード例 #6
0
        public object?Parse(IArgument argument, IEnumerable <string> values)
        {
            // TODO: when _type & values is IEnumerable but not ICollection
            //       DO NOT enumerate values here as it could be a stream.
            var listInstance = _type.IsArray
                ? new ArrayList()
                : _type.IsCollection()
                    ? CreateGenericList()
                    : null;

            if (listInstance == null)
            {
                return(values);
            }

            foreach (string stringValue in values)
            {
                listInstance.Add(_argumentTypeDescriptor.ParseString(argument, stringValue));
            }

            return(_type.IsArray
                ? ((ArrayList)listInstance).ToArray(_underlyingType)
                : listInstance);
        }
コード例 #7
0
 public object Parse(IArgument argument, IEnumerable <string> values)
 {
     return(_argumentTypeDescriptor.ParseString(argument,
                                                values.SingleOrDefaultOrThrow(() => ThrowMultiForSingleEx(argument))));
 }
コード例 #8
0
 public object Parse(IArgument argument, IEnumerable <string> values)
 {
     return(_argumentTypeDescriptor.ParseString(argument, values.SingleOrDefault()));
 }