Beispiel #1
0
        public void ConversionTest()
        {
            var f = new FormattableFormatter <UserDefinedFormattable>();
            IFormatterEx <UserDefinedDrivedFormattable> fobj = f;

            Assert.AreEqual("Hello world!", fobj.Format(new UserDefinedDrivedFormattable()));
        }
Beispiel #2
0
 /// <inhertidoc/>
 public bool TryGetProvider(Type t, out IFormatterEx formatter)
 {
     if (t.IsArray)
     {
         if (t.GetArrayRank() == 1 && t.GetElementType() == typeof(char))
         {
             formatter = new CharEnumerableFormatter();
         }
         else
         {
             formatter = new ArrayFormatter();
         }
         return(true);
     }
     else if (typeof(IEnumerable).IsAssignableFrom(t))
     {
         if (typeof(IEnumerable <char>).IsAssignableFrom(t))
         {
             formatter = new CharEnumerableFormatter();
         }
         else
         {
             formatter = new EnumerableFormatter();
         }
         return(true);
     }
     else
     {
         formatter = null;
         return(false);
     }
 }
 /// <summary>
 /// Returns a humanize string with a formatter that represents the current object.
 /// </summary>
 /// <param name="obj">The current object.</param>
 /// <param name="formatter">The formatter.</param>
 /// <returns>A humanize string.</returns>
 /// <exception cref="ArgumentNullException">When <paramref name="obj"/> is <see langword="null"/> (<see langword="Nothing"/> in Visual Basic).</exception>
 public static string ToStringEx(this object obj, IFormatterEx formatter)
 {
     if (formatter == null)
     {
         return(obj.ToStringEx());
     }
     else
     {
         return(formatter.Format(obj));
     }
 }
 /// <summary>
 /// Returns a humanize string with a formatter that represents the current object.
 /// </summary>
 /// <typeparam name="T">The type of the object.</typeparam>
 /// <param name="obj">The current object.</param>
 /// <param name="formatter">The formatter.</param>
 /// <returns>A humanize string.</returns>
 /// <exception cref="ArgumentNullException">When <paramref name="obj"/> is <see langword="null"/> (<see langword="Nothing"/> in Visual Basic).</exception>
 public static string ToStringEx <T>(this T obj, IFormatterEx <T> formatter)
 {
     if (formatter == null || !formatter.TargetType.IsAssignableFrom(typeof(T)))
     {
         return(obj.ToStringEx());
     }
     else
     {
         return(formatter.Format(obj));
     }
 }
 /// <inhertidoc/>
 public bool TryGetProvider(Type t, out IFormatterEx formatter)
 {
     if (t.FullName.StartsWith("System.Tuple`") || t.FullName.StartsWith("System.ValueTuple`"))
     {
         formatter = new TupleFormatter();
         return(true);
     }
     else
     {
         formatter = null;
         return(false);
     }
 }
 /// <inhertidoc/>
 public bool TryGetProvider(Type t, out IFormatterEx formatter)
 {
     if (t.GetMethod("ToString", Array.Empty <Type>()).DeclaringType == t)
     {
         formatter = new FuncFormatter <object>(obj => obj.ToString());
     }
     else if (Attribute.GetCustomAttribute(t, typeof(DefaultFormatterAttribute)) is DefaultFormatterAttribute attr)
     {
         formatter = (IFormatterEx)Activator.CreateInstance(attr.FormatterType);
     }
     else
     {
         formatter = new ReflectionFormatter();
     }
     return(true);
 }
 /// <inhertidoc/>
 public bool TryGetProvider(Type t, out IFormatterEx formatter)
 {
     if (t.FullName.StartsWith("System.Memory`1"))
     {
         Type[] types = t.GenericTypeArguments;
         formatter = (IFormatterEx)Activator.CreateInstance(Type.GetType("ToStringEx.Memory.MemoryFormatter`1").MakeGenericType(types));
         return(true);
     }
     else if (t.FullName.StartsWith("System.ReadOnlyMemory`1"))
     {
         Type[] types = t.GenericTypeArguments;
         formatter = (IFormatterEx)Activator.CreateInstance(Type.GetType("ToStringEx.Memory.ReadOnlyMemoryFormatter`1").MakeGenericType(types));
         return(true);
     }
     else
     {
         formatter = null;
         return(false);
     }
 }
Beispiel #8
0
 /// <summary>
 /// Initializes an instance of <see cref="ReadOnlyMemoryFormatter{T}"/> with a formatter.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 public ReadOnlyMemoryFormatter(IFormatterEx <T> formatter) : this(formatter, 0)
 {
 }
 /// <summary>
 /// Initializes an instance of <see cref="ArrayFormatter"/>.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 /// <param name="multiLine">Whether to format the array in multi-line.</param>
 /// <param name="maxCount">The maximum count of each rank, or every rank if there's only one element.</param>
 public ArrayFormatter(IFormatterEx formatter, bool multiLine = false, params int[] maxCount) : base(formatter, multiLine, maxCount)
 {
 }
Beispiel #10
0
 /// <summary>
 /// Initializes an instance of <see cref="SpanFormatter{T}"/> with a formatter.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 public SpanFormatter(IFormatterEx <T> formatter) : this(formatter, 0)
 {
 }
 /// <summary>
 /// Initializes an instance of <see cref="EnumerableFormatterBase"/>
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 /// <param name="maxCount">Maximum count of the elements.</param>
 public EnumerableFormatterBase(IFormatterEx formatter, int maxCount) : base(formatter) => MaxCount = maxCount;
 /// <summary>
 /// Initializes an instance of <see cref="EnumerableFormatter"/> with a formatter and maximum count.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 /// <param name="maxCount">Maximum count of the elements.</param>
 public EnumerableFormatter(IFormatterEx formatter, int maxCount) : base(formatter, maxCount)
 {
 }
 /// <summary>
 /// Initializes an instance of <see cref="EnumerableFormatter"/> with a formatter.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 public EnumerableFormatter(IFormatterEx formatter) : this(formatter, 0)
 {
 }
Beispiel #14
0
 /// <summary>
 /// Initializes an instance of <see cref="SequenceFormatterBase"/> with a formatter.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 public SequenceFormatterBase(IFormatterEx formatter) => Formatter = formatter;
Beispiel #15
0
 /// <summary>
 /// Initializes an instance of <see cref="SpanFormatter{T}"/> with a formatter and maximum count.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 /// <param name="maxCount">Maximum count of the elements.</param>
 public SpanFormatter(IFormatterEx <T> formatter, int maxCount) : base(formatter, maxCount)
 {
 }
 /// <summary>
 /// Initializes an instance of <see cref="ArrayFormatterBase"/>.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 /// <param name="multiLine">Whether to format the array in multi-line.</param>
 /// <param name="maxCount">The maximum count of each rank, or every rank if there's only one element.</param>
 public ArrayFormatterBase(IFormatterEx formatter, bool multiLine, int[] maxCount) : base(formatter)
 {
     MultiLine = multiLine;
     MaxCount  = maxCount;
 }
Beispiel #17
0
 /// <summary>
 /// Initializes an instance of <see cref="MemoryFormatter{T}"/> with a formatter and maximum count.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 /// <param name="maxCount">Maximum count of the elements.</param>
 public MemoryFormatter(IFormatterEx <T> formatter, int maxCount) : base(formatter, maxCount)
 {
 }
Beispiel #18
0
 /// <summary>
 /// Initializes an instance of <see cref="MemoryFormatter{T}"/> with a formatter.
 /// </summary>
 /// <param name="formatter">The formatter for each element.</param>
 public MemoryFormatter(IFormatterEx <T> formatter) : this(formatter, 0)
 {
 }
Beispiel #19
0
 /// <summary>
 /// Initializes an instance of <see cref="MemoryFormatterBase{T}"/> with a formatter.
 /// </summary>
 /// <param name="f">The formatter for each element.</param>
 /// <param name="maxCount">Maximum count of the elements.</param>
 public MemoryFormatterBase(IFormatterEx <T> f, int maxCount) => formatter = new SpanFormatter <T>(f, maxCount);