Esempio n. 1
0
        /// <summary>
        ///     Creates new context object for the next level of recursion
        /// </summary>
        /// <param name="fieldName">field name being walked at next recursion level</param>
        /// <returns>new context</returns>
        public DumperContext RecursionCopy(string fieldName)
        {
            var context = new DumperContext(this.MaxDepth, this.writer)
            {
                CurrentDepth         = (ushort)(this.CurrentDepth + 1),
                FieldName            = fieldName,
                Prefix               = this.Prefix,
                EnumerateCollections = this.EnumerateCollections,
            };

            return(context);
        }
Esempio n. 2
0
        /// <summary>
        ///     writes/walks the element into a context
        /// </summary>
        /// <param name="element">element to write/walk</param>
        /// <param name="context">current dump context</param>
        public static void Write(object?element, DumperContext context)
        {
            context = Arguments.EnsureNotNull(context, nameof(context));

            if (element == null)
            {
                DumpHelper.WriteHeader(context);
                DumpHelper.WriteValue("<NULL>", context);
                return;
            }

            if (context.CurrentDepth == context.MaxDepth)
            {
                DumpHelper.WriteHeader(context);
                DumpHelper.WriteValue(element.ToString() !, context);
                return;
            }

            var elementType = element.GetType();

            if (DumpHelper.IsSimpleType(elementType))
            {
                DumpHelper.WriteHeader(context);
                DumpHelper.WriteValue(element, context);
                return;
            }

            if (element is IEnumerable)
            {
                if (!context.EnumerateCollections)
                {
                    return;
                }

                // TODO: enumerate
            }

            var properties = elementType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (var property in properties)
            {
                var newValue = property.GetValue(element);
                DumpHelper.Write(newValue, context.RecursionCopy(property.Name));
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     writes/walks the element's value into a context
        /// </summary>
        /// <param name="element">element to write/walk</param>
        /// <param name="context">current dump context</param>
        private static void WriteValue(object element, DumperContext context)
        {
            Arguments.NotNull(context, nameof(context));

            if (element == null)
            {
                context.Append("null");
            }
            else if (element is DateTime time)
            {
                context.Append(time.ToShortDateString());
            }
            else if (element is char charElement)
            {
                if (charElement == '\0')
                {
                    context.Append("''");
                }
                else
                {
                    context.Append("'" + charElement + "'");
                }
            }
            else if (element is ValueType || element is string)
            {
                context.Append(element.ToString() !);
            }
            else if (element is IEnumerable)
            {
                context.Append("...");
            }
            else
            {
                context.Append("{ }");
            }

            context.Append(Environment.NewLine);
        }
Esempio n. 4
0
        /// <summary>
        ///     writes/walks the element's name/header into a context
        /// </summary>
        /// <param name="context">current dump context</param>
        private static void WriteHeader(DumperContext context)
        {
            Arguments.NotNull(context, nameof(context));

            context.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}:", new string(' ', context.CurrentDepth), context.FieldName);
        }