Exemplo n.º 1
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("}");
        }
Exemplo n.º 2
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);
        }