/// <summary> /// Function called to convert property values to string. If no converter is /// specified, then .ToString is called /// </summary> /// <param name="apiField">API field attribute</param> /// <param name="propertyType">property type information</param> /// <param name="value">current value</param> /// <returns>converted value</returns> private string ConvertPropertyValue(APIField apiField, Type propertyType, object value) { string convertedValue = null; bool isConverted = false; if (apiField.Converter != null) { ITypeToStringConverter converter = Activator.CreateInstance(apiField.Converter) as ITypeToStringConverter; if (converter != null && converter.CanConvert(propertyType)) { convertedValue = converter.Convert(value); isConverted = true; } } if (!isConverted) { convertedValue = value.ToString(); } return(convertedValue); }
/// <summary> /// Function called for each property in a query object. It extracts the different properties /// with <see cref="APIField"/> attribute and, if needed, calls appropiate converter to convert property value to string /// </summary> /// <param name="propertyInfo">property type information</param> /// <param name="query">query object</param> /// <param name="collection">collection to add the converted values</param> private void ConvertProperty(PropertyInfo propertyInfo, object query, NameValueCollection collection) { var attributes = propertyInfo.GetCustomAttributes(true); foreach (var attribute in attributes) { APIField nameAttr = attribute as APIField; if (nameAttr != null) { object value = propertyInfo.GetValue(query); if (value != null) { var convertedValue = ConvertPropertyValue(nameAttr, propertyInfo.PropertyType, value); if (!string.IsNullOrEmpty(convertedValue)) { collection.Add(nameAttr.ApiName, convertedValue); } } // No need to continue. Only one APIField attribute per property return; } } }