Пример #1
0
        public string FormatValue(ResolutionContext context)
        {
            Type valueType = context.SourceType;
            object valueToFormat = context.SourceValue;
            IFormatterConfiguration typeSpecificFormatterConfig = null;
            string formattedValue = context.SourceValue.ToNullSafeString();

            if (context.PropertyMap != null)
            {
                foreach (IValueFormatter formatter in context.PropertyMap.GetFormatters())
                {
                    formattedValue = formatter.FormatValue(context.CreateValueContext(valueToFormat));
                    valueToFormat = formattedValue;
                }

                if (_formatterConfiguration.GetTypeSpecificFormatters().TryGetValue(valueType, out typeSpecificFormatterConfig))
                {
                    if (!context.PropertyMap.FormattersToSkipContains(typeSpecificFormatterConfig.GetType()))
                    {
                        var typeSpecificFormatter = new ValueFormatter(typeSpecificFormatterConfig);
                        formattedValue = typeSpecificFormatter.FormatValue(context);
                        valueToFormat = formattedValue;
                    }
                }
            }
            else if (_formatterConfiguration.GetTypeSpecificFormatters().TryGetValue(valueType, out typeSpecificFormatterConfig))
            {
                var typeSpecificFormatter = new ValueFormatter(typeSpecificFormatterConfig);
                formattedValue = typeSpecificFormatter.FormatValue(context);
                valueToFormat = formattedValue;
            }

            foreach (IValueFormatter formatter in _formatterConfiguration.GetFormatters())
            {
                Type formatterType = GetFormatterType(formatter);
                if (CheckPropertyMapSkipList(context, formatterType) &&
                    CheckTypeSpecificSkipList(typeSpecificFormatterConfig, formatterType))
                {
                    formattedValue = formatter.FormatValue(context.CreateValueContext(valueToFormat));
                    valueToFormat = formattedValue;
                }
            }

            return formattedValue;
        }