コード例 #1
0
        /// <summary>
        /// format:{$key:format}
        /// </summary>
        public static string FormatStringWithKey(this string format, IFormatProvider formatProvider, IKeyFormatStringValueProvider valueProvider)
        {
            string result;

            result = keyFormatStringRegex.Replace(format, (m) =>
            {
                string paramName = m.Groups["key"].Value;
                object value;
                string ret = null;


                if (string.IsNullOrEmpty(paramName))
                {
                    throw new FormatException("format error:" + m.Value);
                }

                value = valueProvider.GetFormatValue(paramName);

                if (value != null)
                {
                    object val;
                    val            = value;
                    string formats = m.Groups["format"].Value;

                    foreach (var fmt in formats.Split(':'))
                    {
                        ret = string.Format(formatProvider, "{0:" + fmt + "}", val);
                        val = ret;
                    }
                }
                else
                {
                    ret = string.Empty;
                }

                return(ret);
            });
            return(result);
        }
 public DictionaryFormatStringValueProvider(Dictionary <string, object> values, IKeyFormatStringValueProvider baseProvider)
 {
     this.values       = values;
     this.baseProvider = baseProvider;
 }
コード例 #3
0
 /// <summary>
 /// format:{$key:format}
 /// </summary>
 public static string FormatStringWithKey(this string format, IKeyFormatStringValueProvider valueProvider)
 {
     return(FormatStringWithKey(format, FormatProvider, valueProvider));
 }