public IDictionary <string, string> GetKeyValuePairs()
        {
            Dictionary <string, string> queryParameters = new Dictionary <string, string>();

            foreach (var pair in this.AttributedPublicProperties)
            {
                PropertyInfo property = pair.Item1;
                QueryStringParameterAttribute attribute = pair.Item2;
                object value = property.GetValue(this.Object, null);

                // 'Required' check
                if (attribute.IsRequired && value == null)
                {
                    string propertyFullName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", property.GetType().FullName, property.Name);
                    throw new ArgumentException("Got null/unset value for a required query parameter.", propertyFullName);
                }

                // Serialize
                if (value != null)
                {
                    string keyStr = attribute.Name;
                    string valueStr;
                    if (attribute.ConverterType == null)
                    {
                        valueStr = value.ToString();
                    }
                    else
                    {
                        IQueryStringConverter converter = this.QueryStringConverterInstanceFactory.GetConverterInstance(attribute.ConverterType);
                        valueStr = this.ConvertValue(converter, value);

                        if (converter.ChangesKey())
                        {
                            keyStr = converter.GetKey(value);
                        }

                        if (valueStr == null)
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                              "Got null from value converter '{0}'", attribute.ConverterType.FullName));
                        }
                    }

                    queryParameters[keyStr] = valueStr;
                }
            }

            return(queryParameters);
        }
Esempio n. 2
0
        public IDictionary <string, string[]> GetKeyValuePairs()
        {
            Dictionary <string, string[]> queryParameters = new Dictionary <string, string[]>();

            foreach (var pair in this.AttributedPublicProperties)
            {
                PropertyInfo property = pair.Item1;
                QueryStringParameterAttribute attribute = pair.Item2;
                object value = property.GetValue(this.Object, null);

                // 'Required' check
                if (attribute.IsRequired && value == null)
                {
                    string propertyFullName = $"{property.GetType().FullName}.{property.Name}";
                    throw new ArgumentException("Got null/unset value for a required query parameter.", propertyFullName);
                }

                // Serialize
                if (attribute.IsRequired || !IsDefaultOfType(value))
                {
                    string   keyStr = attribute.Name;
                    string[] valueStr;
                    if (attribute.ConverterType == null)
                    {
                        valueStr = new[] { value.ToString() };
                    }
                    else
                    {
                        IQueryStringConverter converter = this.QueryStringConverterInstanceFactory.GetConverterInstance(attribute.ConverterType);
                        valueStr = this.ConvertValue(converter, value);

                        if (valueStr == null)
                        {
                            throw new InvalidOperationException($"Got null from value converter '{attribute.ConverterType.FullName}'");
                        }
                    }

                    queryParameters[keyStr] = valueStr;
                }
            }

            return(queryParameters);
        }