Exemplo n.º 1
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            int startCount = formattedGraph.LineCount;
            IEnumerable <KeyValuePair <object, object> > collection = AsEnumerable((IDictionary)value);

            using var iterator = new Iterator <KeyValuePair <object, object> >(collection, MaxItems);
            while (iterator.MoveNext())
            {
                if (iterator.IsFirst)
                {
                    formattedGraph.AddFragment("{");
                }

                if (!iterator.HasReachedMaxItems)
                {
                    var index = iterator.Index.ToString(CultureInfo.InvariantCulture);
                    formattedGraph.AddFragment("[");
                    formatChild(index + ".Key", iterator.Current.Key, formattedGraph);
                    formattedGraph.AddFragment("] = ");
                    formatChild(index + ".Value", iterator.Current.Value, formattedGraph);
                }
                else
                {
                    using IDisposable _ = formattedGraph.WithIndentation();
                    string moreItemsMessage = $"…{collection.Count() - MaxItems} more…";
                    AddLineOrFragment(formattedGraph, startCount, moreItemsMessage);
                }

                if (iterator.IsLast)
                {
                    AddLineOrFragment(formattedGraph, startCount, "}");
                }
                else
                {
                    formattedGraph.AddFragment(", ");
                }
            }

            if (iterator.IsEmpty)
            {
                formattedGraph.AddFragment("{empty}");
            }
        }
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            int startCount = formattedGraph.LineCount;
            IEnumerable <object> collection = ((IEnumerable)value).Cast <object>();

            using var iterator = new Iterator <object>(collection, MaxItems);
            while (iterator.MoveNext())
            {
                if (iterator.IsFirst)
                {
                    formattedGraph.AddFragment("{");
                }

                if (!iterator.HasReachedMaxItems)
                {
                    formatChild(iterator.Index.ToString(CultureInfo.InvariantCulture), iterator.Current, formattedGraph);
                }
                else
                {
                    using IDisposable _ = formattedGraph.WithIndentation();
                    string moreItemsMessage = value is ICollection c ? $"…{c.Count - MaxItems} more…" : "…more…";
                    AddLineOrFragment(formattedGraph, startCount, moreItemsMessage);
                }

                if (iterator.IsLast)
                {
                    AddLineOrFragment(formattedGraph, startCount, "}");
                }
                else
                {
                    formattedGraph.AddFragment(", ");
                }
            }

            if (iterator.IsEmpty)
            {
                formattedGraph.AddFragment("{empty}");
            }
        }