public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var timeSpan = (TimeSpan)value;

            if (timeSpan == TimeSpan.MinValue)
            {
                formattedGraph.AddFragment("min time span");
                return;
            }

            if (timeSpan == TimeSpan.MaxValue)
            {
                formattedGraph.AddFragment("max time span");
                return;
            }

            List <string> fragments = GetNonZeroFragments(timeSpan);

            if (!fragments.Any())
            {
                formattedGraph.AddFragment("default");
            }

            string sign = (timeSpan.Ticks >= 0) ? string.Empty : "-";

            if (fragments.Count == 1)
            {
                formattedGraph.AddFragment(sign + fragments.Single());
            }
            else
            {
                formattedGraph.AddFragment(sign + fragments.JoinUsingWritingStyle());
            }
        }
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            MethodInfo method = GetFormatter(value);

            object[] parameters = new[] { value, formattedGraph };

            method.Invoke(null, parameters);
        }
Beispiel #3
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var element = (XElement)value;

            formattedGraph.AddFragment(element.HasElements
                ? FormatElementWithChildren(element)
                : FormatElementWithoutChildren(element));
        }
        /// <inheritdoc />
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            string typePart  = value.GetType().Name;
            string namePart  = value.ToString().Replace(", ", "|", StringComparison.Ordinal);
            string valuePart = Convert.ToDecimal(value, CultureInfo.InvariantCulture)
                               .ToString(CultureInfo.InvariantCulture);

            formattedGraph.AddFragment($"{typePart}.{namePart} {{value: {valuePart}}}");
        }
Beispiel #5
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var lambdaExpression = (LambdaExpression)value;

            var reducedExpression = ReduceConstantSubExpressions(lambdaExpression.Body);

            if (reducedExpression is BinaryExpression binaryExpression && binaryExpression.NodeType == ExpressionType.AndAlso)
            {
                var subExpressions = ExtractChainOfExpressionsJoinedWithAndOperator(binaryExpression);
                formattedGraph.AddFragment(string.Join(" AndAlso ", subExpressions.Select(e => e.ToString())));
            }
 private static void AddLineOrFragment(FormattedObjectGraph formattedGraph, int startCount, string fragment)
 {
     if (formattedGraph.LineCount > (startCount + 1))
     {
         formattedGraph.AddLine(fragment);
     }
     else
     {
         formattedGraph.AddFragment(fragment);
     }
 }
 public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
 {
     if (value is Task task)
     {
         formatChild("type", task.GetType(), formattedGraph);
         formattedGraph.AddFragment($" {{Status={task.Status}}}");
     }
     else
     {
         formattedGraph.AddFragment("<null>");
     }
 }
Beispiel #8
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var document = (XDocument)value;

            if (document.Root is not null)
            {
                formatChild("root", document.Root, formattedGraph);
            }
            else
            {
                formattedGraph.AddFragment("[XML document without root element]");
            }
        }
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var reader = (XmlReader)value;

            if (reader.ReadState == ReadState.Initial)
            {
                reader.Read();
            }

            var result = "\"" + reader.ReadOuterXml() + "\"";

            formattedGraph.AddFragment(result);
        }
Beispiel #10
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var exception = (Exception)value;

            formattedGraph.AddFragment(Invariant($"{exception.GetType().FullName} with message \"{exception.Message}\""));

            if (exception.StackTrace is not null)
            {
                foreach (string line in exception.StackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
                {
                    formattedGraph.AddLine("  " + line);
                }
            }
        }
Beispiel #11
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}");
            }
        }
Beispiel #12
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            float singleValue = (float)value;

            if (float.IsPositiveInfinity(singleValue))
            {
                formattedGraph.AddFragment(nameof(Single) + "." + nameof(float.PositiveInfinity));
            }
            else if (float.IsNegativeInfinity(singleValue))
            {
                formattedGraph.AddFragment(nameof(Single) + "." + nameof(float.NegativeInfinity));
            }
            else if (float.IsNaN(singleValue))
            {
                formattedGraph.AddFragment(singleValue.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                formattedGraph.AddFragment(singleValue.ToString("R", CultureInfo.InvariantCulture) + "F");
            }
        }
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var exception = (AggregateException)value;

            if (exception.InnerExceptions.Count == 1)
            {
                formattedGraph.AddFragment("(aggregated) ");

                formatChild("inner", exception.InnerException, formattedGraph);
            }
            else
            {
                formattedGraph.AddLine(Invariant($"{exception.InnerExceptions.Count} (aggregated) exceptions:"));

                foreach (Exception innerException in exception.InnerExceptions)
                {
                    formattedGraph.AddLine(string.Empty);
                    formatChild("InnerException", innerException, formattedGraph);
                }
            }
        }
Beispiel #14
0
        private void WriteTypeAndMemberValues(object obj, FormattedObjectGraph formattedGraph, FormatChild formatChild)
        {
            Type type = obj.GetType();

            formattedGraph.AddLine(TypeDisplayName(type));
            formattedGraph.AddLine("{");

            MemberInfo[] members = GetMembers(type);
            using var iterator = new Iterator <MemberInfo>(members.OrderBy(mi => mi.Name));
            while (iterator.MoveNext())
            {
                WriteMemberValueTextFor(obj, iterator.Current, formattedGraph, formatChild);

                if (!iterator.IsLast)
                {
                    formattedGraph.AddFragment(", ");
                }
            }

            formattedGraph.AddFragmentOnNewLine("}");
        }
Beispiel #15
0
        private static void WriteMemberValueTextFor(object value, MemberInfo member, FormattedObjectGraph formattedGraph, FormatChild formatChild)
        {
            object memberValue;

            try
            {
                memberValue = member switch
                {
                    FieldInfo fi => fi.GetValue(value),
                    PropertyInfo pi => pi.GetValue(value),
                    _ => throw new InvalidOperationException()
                };
            }
            catch (Exception ex)
            {
                ex          = (ex as TargetInvocationException)?.InnerException ?? ex;
                memberValue = $"[Member '{member.Name}' threw an exception: '{ex.Message}']";
            }

            formattedGraph.AddFragmentOnNewLine($"{new string(' ', FormattedObjectGraph.SpacesPerIndentation)}{member.Name} = ");
            formatChild(member.Name, memberValue, formattedGraph);
        }
        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}");
            }
        }
Beispiel #17
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            if (value.GetType() == typeof(object))
            {
                formattedGraph.AddFragment($"System.Object (HashCode={value.GetHashCode()})");
                return;
            }

            if (HasDefaultToStringImplementation(value))
            {
                WriteTypeAndMemberValues(value, formattedGraph, formatChild);
            }
            else
            {
                if (context.UseLineBreaks)
                {
                    formattedGraph.AddLine(value.ToString());
                }
                else
                {
                    formattedGraph.AddFragment(value.ToString());
                }
            }
        }
Beispiel #18
0
 public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
 {
     formattedGraph.AddFragment(((XAttribute)value).ToString());
 }
 public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
 {
     formattedGraph.AddFragment(Format(value));
 }
Beispiel #20
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var arr = (Array)value;

            if (arr.Length == 0)
            {
                formattedGraph.AddFragment("{empty}");
                return;
            }

            int[] dimensionIndices = Enumerable.Range(0, arr.Rank).Select(dimension => arr.GetLowerBound(dimension)).ToArray();

            int         currentLoopIndex = 0;
            IEnumerator enumerator       = arr.GetEnumerator();

            // Emulate n-ary loop
            while (currentLoopIndex >= 0)
            {
                int currentDimensionIndex = dimensionIndices[currentLoopIndex];

                if (IsFirstIteration(arr, currentDimensionIndex, currentLoopIndex))
                {
                    formattedGraph.AddFragment("{");
                }

                if (IsInnerMostLoop(arr, currentLoopIndex))
                {
                    enumerator.MoveNext();
                    formatChild(string.Join("-", dimensionIndices), enumerator.Current, formattedGraph);
                    if (!IsLastIteration(arr, currentDimensionIndex, currentLoopIndex))
                    {
                        formattedGraph.AddFragment(", ");
                    }

                    ++dimensionIndices[currentLoopIndex];
                }
                else
                {
                    ++currentLoopIndex;
                    continue;
                }

                while (IsLastIteration(arr, currentDimensionIndex, currentLoopIndex))
                {
                    formattedGraph.AddFragment("}");

                    // Reset current loop's variable to start value ...and move to outer loop
                    dimensionIndices[currentLoopIndex] = arr.GetLowerBound(currentLoopIndex);
                    --currentLoopIndex;

                    if (currentLoopIndex < 0)
                    {
                        break;
                    }

                    currentDimensionIndex = dimensionIndices[currentLoopIndex];
                    if (!IsLastIteration(arr, currentDimensionIndex, currentLoopIndex))
                    {
                        formattedGraph.AddFragment(", ");
                    }

                    ++dimensionIndices[currentLoopIndex];
                }
            }
        }
Beispiel #21
0
 public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
 {
     formattedGraph.AddFragment("0x" + ((byte)value).ToString("X2", CultureInfo.InvariantCulture));
 }
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            DateTimeOffset dateTimeOffset;
            bool           significantOffset = false;

            if (value is DateTime dateTime)
            {
                dateTimeOffset = dateTime.ToDateTimeOffset();
            }
            else
            {
                dateTimeOffset    = (DateTimeOffset)value;
                significantOffset = true;
            }

            formattedGraph.AddFragment("<");

            bool hasDate = HasDate(dateTimeOffset);

            if (hasDate)
            {
                formattedGraph.AddFragment(dateTimeOffset.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
            }

            bool hasTime = HasTime(dateTimeOffset);

            if (hasTime)
            {
                if (hasDate)
                {
                    formattedGraph.AddFragment(" ");
                }

                if (HasNanoSeconds(dateTimeOffset))
                {
                    formattedGraph.AddFragment(dateTimeOffset.ToString("HH:mm:ss.fffffff", CultureInfo.InvariantCulture));
                }
                else if (HasMicroSeconds(dateTimeOffset))
                {
                    formattedGraph.AddFragment(dateTimeOffset.ToString("HH:mm:ss.ffffff", CultureInfo.InvariantCulture));
                }
                else if (HasMilliSeconds(dateTimeOffset))
                {
                    formattedGraph.AddFragment(dateTimeOffset.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture));
                }
                else
                {
                    formattedGraph.AddFragment(dateTimeOffset.ToString("HH:mm:ss", CultureInfo.InvariantCulture));
                }
            }

            if (dateTimeOffset.Offset > TimeSpan.Zero)
            {
                formattedGraph.AddFragment(" +");
                formatChild("offset", dateTimeOffset.Offset, formattedGraph);
            }
            else if (dateTimeOffset.Offset < TimeSpan.Zero)
            {
                formattedGraph.AddFragment(" ");
                formatChild("offset", dateTimeOffset.Offset, formattedGraph);
            }
            else if (significantOffset && (hasDate || hasTime))
            {
                formattedGraph.AddFragment(" +0h");
            }
            else
            {
                // No offset added, since it was deemed unnecessary
            }

            if (!hasDate && !hasTime)
            {
                if (HasMilliSeconds(dateTimeOffset))
                {
                    formattedGraph.AddFragment("0001-01-01 00:00:00." + dateTimeOffset.ToString("fff", CultureInfo.InvariantCulture));
                }
                else
                {
                    formattedGraph.AddFragment("0001-01-01 00:00:00.000");
                }
            }

            formattedGraph.AddFragment(">");
        }
Beispiel #23
0
 public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
 {
     formattedGraph.AddFragment(((PropertyInfo)value).Name);
 }
Beispiel #24
0
 public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
 {
     formattedGraph.AddFragment(((decimal)value).ToString(CultureInfo.InvariantCulture) + "M");
 }
Beispiel #25
0
 public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
 {
     formattedGraph.AddFragment(value.ToString().Replace(" = ", " == ", StringComparison.Ordinal));
 }
Beispiel #26
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var dateOnly = (DateOnly)value;

            formattedGraph.AddFragment(dateOnly.ToString("<yyyy-MM-dd>", CultureInfo.InvariantCulture));
        }
Beispiel #27
0
        public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
        {
            var timeOnly = (TimeOnly)value;

            formattedGraph.AddFragment(timeOnly.ToString("<HH:mm:ss.fff>", CultureInfo.InvariantCulture));
        }