Ejemplo n.º 1
0
        /// <summary>
        /// Formats an object and writes it to the specified writer.
        /// </summary>
        /// <param name="obj">The object to be formatted.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="mimeType">The mime type to format to.</param>
        public static void FormatTo(
            FormatContext context,
            T obj,
            TextWriter writer,
            string mimeType = PlainTextFormatter.MimeType)
        {
            if (obj == null)
            {
                var formatter = Formatter.GetPreferredFormatterFor(typeof(T), mimeType);
                formatter.Format(context, null, writer);
                return;
            }

            using var _ = Formatter.RecursionCounter.Enter();

            // find a formatter for the object type, and possibly register one on the fly
            if (Formatter.RecursionCounter.Depth <= Formatter.RecursionLimit)
            {
                var formatter = Formatter.GetPreferredFormatterFor(typeof(T), mimeType);
                formatter.Format(context, obj, writer);
            }
            else
            {
                PlainTextFormatter <T> .Default.Format(context, obj, writer);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Formats an object and writes it to the specified writer.
        /// </summary>
        /// <param name="obj">The object to be formatted.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="context">The context for the current format operation.</param>
        /// <param name="mimeType">The mime type to format to.</param>
        public static void FormatTo(
            T obj,
            FormatContext context,
            string mimeType = PlainTextFormatter.MimeType)
        {
            if (obj is null)
            {
                var formatter = Formatter.GetPreferredFormatterFor(typeof(T), mimeType);
                formatter.Format(null, context);
                return;
            }

            using var _ = Formatter.RecursionCounter.Enter();

            if (Formatter.RecursionCounter.Depth <= Formatter.RecursionLimit)
            {
                var formatter = Formatter.GetPreferredFormatterFor(typeof(T), mimeType);
                formatter.Format(obj, context);
            }
            else
            {
                PlainTextFormatter <T> .Default.Format(obj, context);
            }
        }
Ejemplo n.º 3
0
 public static ITypeFormatter GetPreferredFormatterFor(Type type) =>
 Formatter.GetPreferredFormatterFor(type, MimeType);
Ejemplo n.º 4
0
 public static ITypeFormatter GetPreferredFormatterFor(Type type)
 {
     return(Formatter.GetPreferredFormatterFor(type, MimeType));
 }
Ejemplo n.º 5
0
        internal static HtmlFormatter <T> CreateForAnyEnumerable(bool includeInternals)
        {
            Func <T, IEnumerable> getKeys   = null;
            Func <T, IEnumerable> getValues = instance => (IEnumerable)instance;

            var dictType =
                typeof(T).GetAllInterfaces()
                .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                ??
                typeof(T).GetAllInterfaces()
                .FirstOrDefault(i => i == typeof(IDictionary));

            if (dictType is not null)
            {
                var keysProperty = dictType.GetProperty("Keys");
                getKeys = instance => (IEnumerable)keysProperty.GetValue(instance, null);

                var valuesProperty = dictType.GetProperty("Values");
                getValues = instance => (IEnumerable)valuesProperty.GetValue(instance, null);
            }

            return(new HtmlFormatter <T>(BuildTable));

            bool BuildTable(T source, FormatContext context)
            {
                using var _ = context.IncrementTableDepth();

                if (context.TableDepth > 1)
                {
                    HtmlFormatter.FormatAndStyleAsPlainText(source, context);
                    return(true);
                }

                var(rowData, remainingCount) = getValues(source)
                                               .Cast <object>()
                                               .Select((v, i) => (v, i))
                                               .TakeAndCountRemaining(Formatter.ListExpansionLimit);

                if (rowData.Count == 0)
                {
                    context.Writer.Write(i("(empty)"));
                    return(true);
                }

                var valuesByHeader    = new Dictionary <string, Dictionary <int, object> >();
                var headerToSortIndex = new Dictionary <string, (int, int)>();
                var typesAreDifferent = false;
                var types             = new Dictionary <Type, int>();

                foreach (var(value, index) in rowData)
                {
                    IDictionary <string, object> keysAndValues;

                    if (value is { } &&
                        Formatter.GetPreferredFormatterFor(value.GetType(), HtmlFormatter.MimeType) is { } formatter&&
                        formatter.Type == typeof(object))
                    {
                        var destructurer = Destructurer.GetOrCreate(value?.GetType());

                        keysAndValues = destructurer.Destructure(value);
                    }