protected virtual bool IsMatch(Type matchingType, Type currentType, SupurlativeOptions options) { if (matchingType == currentType) { return(true); } return(false); }
public TemplateGenerator(HttpRequestMessage httpRequestMessage, SupurlativeOptions options = null) { if (httpRequestMessage == null) { throw new ArgumentNullException("httpRequestMessage"); } Options = options ?? SupurlativeOptions.Defaults; Options.Validate(); _httpRequestMessage = CloneHttpRequestMessageWithoutConstraints(httpRequestMessage); }
public UrlGenerator( HttpRequestMessage requestMessage, SupurlativeOptions options = null) { if (requestMessage == null) { throw new ArgumentNullException("requestMessage"); } _requestMessage = requestMessage; Options = options ?? SupurlativeOptions.Defaults; Options.Validate(); }
public Generator( HttpRequestMessage requestMessage, SupurlativeOptions options = null) { if (requestMessage == null) { throw new ArgumentNullException("requestMessage"); } var generatorOptions = options ?? SupurlativeOptions.Defaults; generatorOptions.Validate(); Options = generatorOptions; _templateGenerator = new TemplateGenerator(requestMessage, generatorOptions); _urlGenerator = new UrlGenerator(requestMessage, generatorOptions); }
internal static IDictionary <string, object> TraverseForKeys( this object target, SupurlativeOptions options, string parentKey = null) { var kvp = new Dictionary <string, object>(); if (target == null) { return(kvp); } var valueType = target as Type == null ? target.GetType() : target as Type; var properties = valueType .GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(x => x.CanWrite || valueType.CheckIfAnonymousType()); foreach (var property in properties) { var fullPropertyName = parentKey == null ? property.Name : string.Format("{0}{1}{2}", parentKey, options.PropertyNameSeperator, property.Name); object valueOrPropertyType = property.PropertyType; if (target as Type == null) { valueOrPropertyType = property.GetValue(target, null) ?? property.PropertyType; } var formatterAttribute = property.GetCustomAttributes() .Where(x => typeof(BaseFormatterAttribute).IsAssignableFrom(x.GetType())) .Cast <BaseFormatterAttribute>() .FirstOrDefault(); if (formatterAttribute == null) { // find any global formatters formatterAttribute = options .Formatters .Where(x => x.IsMatch(property.PropertyType, options)) .FirstOrDefault(); } if (formatterAttribute != null) { var targetValue = target == null || target as Type != null ? null : property.GetValue(target, null); try { formatterAttribute.Invoke( fullPropertyName, targetValue, property.PropertyType, kvp, options); } catch (Exception ex) { throw new FormatterException( string.Format("There is a problem invoking the formatter: {0}.", formatterAttribute.GetType().FullName), ex); } } else { var kvpValue = (valueOrPropertyType != null && valueOrPropertyType as Type == null ? valueOrPropertyType.ToString() : null); if (property.PropertyType.IsPrimitive || (!string.IsNullOrEmpty(property.PropertyType.Namespace) && property.PropertyType.Namespace.StartsWith("System"))) { kvp.Add(fullPropertyName, kvpValue); } else { var results = TraverseForKeys(valueOrPropertyType, options, fullPropertyName); if (results.Count() == 0) { kvp.Add(fullPropertyName, kvpValue); } else { foreach (var result in results) { kvp.Add(result.Key, result.Value); } } } } } return(kvp.ToDictionary( x => options.LowercaseKeys ? x.Key.ToLower() : x.Key, x => x.Value )); }
public override bool IsMatch(Type currentType, SupurlativeOptions options) { return(IsMatch(_type, currentType, options)); }
public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options) { dictionary.Add(fullPropertyName, _func(value)); }
public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options) { if (value == null) { dictionary.Add(fullPropertyName, null); return; } if (valueType.IsGenericType) { var valueGenericType = valueType.GetGenericArguments().First(); var formatter = options.Formatters .Where(x => x.GetType() != typeof(NullableFormatter)) .Where(x => x.IsMatch(valueGenericType, options)) .FirstOrDefault(); if (formatter == null) { dictionary.Add(fullPropertyName, value); } else { formatter.Invoke(fullPropertyName, value, valueType, dictionary, options); } } }
public override bool IsMatch(Type currentType, SupurlativeOptions options) { return(currentType.IsGenericType && currentType.GetGenericTypeDefinition() == typeof(Nullable <>)); }
public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options) { return; }
public override bool IsMatch(Type currentType, SupurlativeOptions options) { return(false); }
public abstract bool IsMatch(Type currentType, SupurlativeOptions options);
public abstract void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options);