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); }
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); } }
public object?Parse(IArgument argument, IEnumerable <string> values) { var value = values.SingleOrDefaultOrThrow(() => ThrowMultiForSingleEx(argument)); return(value is null ? null : _argumentTypeDescriptor.ParseString(argument, value)); }
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)}"); } }
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)}"); } }
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); }
public object Parse(IArgument argument, IEnumerable <string> values) { return(_argumentTypeDescriptor.ParseString(argument, values.SingleOrDefaultOrThrow(() => ThrowMultiForSingleEx(argument)))); }
public object Parse(IArgument argument, IEnumerable <string> values) { return(_argumentTypeDescriptor.ParseString(argument, values.SingleOrDefault())); }