Example #1
0
        public static PlainTextFormatter <T> CreateForAnyEnumerable(bool _includeInternals)
        {
            return(new PlainTextFormatter <T>((T value, TextWriter writer) =>
            {
                if (value is string)
                {
                    writer.Write(value);
                    return;
                }

                switch (value)
                {
                case IEnumerable enumerable:
                    Formatter.Join(
                        enumerable,
                        writer,
                        Formatter <T> .ListExpansionLimit);
                    break;

                default:
                    writer.Write(value.ToString());
                    break;
                }
            }));
        }
Example #2
0
        public static PlainTextFormatter <T> CreateForAnyEnumerable(bool _includeInternals)
        {
            return(new((value, context) =>
            {
                if (value is string)
                {
                    context.Writer.Write(value);
                    return true;
                }

                switch (value)
                {
                case IEnumerable enumerable:
                    Formatter.Join(enumerable,
                                   context.Writer,
                                   context, Formatter <T> .ListExpansionLimit);
                    break;

                default:
                    context.Writer.Write(value.ToString());
                    break;
                }
                return true;
            }));
        }
Example #3
0
        public static PlainTextFormatter <T> CreateForAnyEnumerable(bool includeInternals = false)
        {
            if (typeof(T) == typeof(string))
            {
                return(new((value, context) =>
                {
                    context.Writer.Write(value);
                    return true;
                }));
            }

            if (typeof(T).GetInterfaces()
                .Where(i => i.IsGenericType)
                .Where(i => i.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                .Select(i => i.GenericTypeArguments[0])
                .FirstOrDefault() is { } t)
            {
                var joinMethod = typeof(Formatter).GetMethod(nameof(Formatter.JoinGeneric), BindingFlags.NonPublic | BindingFlags.Static);

                var genericMethod = joinMethod.MakeGenericMethod(new[] { t });

                var enumerableType = typeof(IEnumerable <>).MakeGenericType(t);

                var delegateType = typeof(Action <, , ,>).MakeGenericType(
                    new[] { enumerableType, typeof(TextWriter), typeof(FormatContext), typeof(int?) });

                var m = genericMethod.CreateDelegate(delegateType);

                return(new((value, context) =>
                {
                    m.DynamicInvoke(value, context.Writer, context, Formatter <T> .ListExpansionLimit);
                    return true;
                }));
            }

            return(new((value, context) =>
            {
                switch (value)
                {
                case IEnumerable enumerable:
                    Formatter.Join(enumerable,
                                   context.Writer,
                                   context, Formatter <T> .ListExpansionLimit);
                    break;

                default:
                    context.Writer.Write(value.ToString());
                    break;
                }

                return true;
            }));
        }
Example #4
0
        internal virtual void WriteDefault(
            T value,
            TextWriter writer)
        {
            if (value is string)
            {
                writer.Write(value);
                return;
            }

            switch (value)
            {
            case IEnumerable enumerable:
                Formatter.Join(
                    enumerable,
                    writer,
                    Formatter <T> .ListExpansionLimit);
                break;

            default:
                writer.Write(value.ToString());
                break;
            }
        }