Example #1
0
 /// <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)
 {
     return(apiField.Converter != null &&
            Activator.CreateInstance(apiField.Converter) is ITypeToStringConverter converter &&
            converter.CanConvert(propertyType)
         ? converter.Convert(value)
         : value.ToString());
 }
        /// <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, IDictionary <string, string> 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;
                }
            }
        }