コード例 #1
0
        public override bool Format(
            T value,
            FormatContext context)
        {
            if (value is null)
            {
                HtmlFormatter.FormatAndStyleAsPlainText(Formatter.NullString, context);
                return(true);
            }

            return(_format(value, context));
        }
コード例 #2
0
        public override bool Format(
            T value,
            FormatContext context)
        {
            using var _ = context.IncrementDepth();

            if (value is null)
            {
                HtmlFormatter.FormatAndStyleAsPlainText(Formatter.NullString, context);
                return(true);
            }

            return(_format(value, context));
        }
コード例 #3
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);
                    }