Exemplo n.º 1
0
 /// <summary>
 /// Removes an objectFormatter from the list of supported formatters
 /// </summary>
 /// <param name="formatter">the formatter that is being disposed</param>
 private static void UnregisterFormatter(ObjectFormatter formatter)
 {
     lock (initializedFormatters)
     {
         if (initializedFormatters.Contains(formatter))
         {
             initializedFormatters.Remove(formatter);
         }
     }
 }
Exemplo n.º 2
0
        public static string DefaultFormat(object value, string format)
        {
            string retVal = string.Empty;

            if (format.Equals("obj", StringComparison.OrdinalIgnoreCase))
            {
                return(ObjectFormatter.FormatObject(value));
            }

            IFormattable formattable = value as IFormattable;

            if (formattable != null)
            {
                retVal = formattable.ToString(format, null);
            }

            return(retVal);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds a formatter to the list of available formatters
        /// </summary>
        /// <param name="formatter">the new initialized formatter</param>
        private static void RegisterFormatter(ObjectFormatter formatter)
        {
            lock (initializedFormatters)
            {
                if (initializedFormatters.Any(n => n.TargetType == formatter.TargetType))
                {
                    throw new InvalidOperationException(
                              $"Can only have one Formatter that formats {formatter.TargetType.FullName}");
                }

                var nextItem =
                    initializedFormatters.Select((o, i) => new { Formatter = o, Index = i })
                    .FirstOrDefault(n => n.Formatter.TargetType.IsAssignableFrom(formatter.TargetType));
                if (nextItem == null)
                {
                    initializedFormatters.Add(formatter);
                }
                else
                {
                    initializedFormatters.Insert(nextItem.Index, formatter);
                }
            }
        }