private string ConvertValue(IQueryStringConverter converter, object value) { if (!converter.CanConvert(value.GetType())) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot convert type {0} using {1}.", value.GetType().FullName, converter.GetType().FullName)); } return(converter.Convert(value)); }
private string[] ConvertValue(IQueryStringConverter converter, object value) { if (!converter.CanConvert(value.GetType())) { throw new InvalidOperationException( $"Cannot convert type {value.GetType().FullName} using {converter.GetType().FullName}."); } return(converter.Convert(value)); }
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); }
public IQueryStringConverter GetConverterInstance(Type t) { IQueryStringConverter cached = GetCachedInstance(t); if (cached != null) { return(cached); } // Create new instance var instance = InitializeConverter(t); // Cache the instance CacheInstance(t, instance); return(instance); }
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); }
private static void CacheInstance(Type t, IQueryStringConverter instance) { ConverterInstanceRegistry[t] = instance; }
private static void CacheInstance(Type t, IQueryStringConverter instance) { ConverterInstanceRegistry[t] = instance; }