Exemple #1
0
        private static void PrettyPrintRecursive(this object obj, StringBuilder sb, int indentation, bool printHeader)
        {
            DescriptionAttribute description;

            if (obj.GetParameters().IsEmpty())
            {
                description = obj.GetType().IsEnum ? ((Enum)obj).GetDescriptionForEnum() : null;
                string toString = obj.ToString();
                if (description == null && toString == obj.GetType().FullName)
                {
                    description = obj.GetDescription();
                }
                sb.Append('=').AppendLine(description == null ? obj.ToString() : description.DisplayName);
                return;
            }

            if (printHeader)
            {
                description = obj.GetDescription();
                sb.Append('=').Append(description == null ? obj.GetType().Name : description.DisplayName);
            }
            if (indentation > 0)
            {
                sb.AppendLine(" {");
            }
            foreach (var param in obj.GetParameters())
            {
                sb.AppendTabs(indentation).Append(param.DisplayName);
                var value = obj.GetProperty(param.Property);
                if (value == null)
                {
                    sb.AppendLine("null");
                }
                else
                {
                    value.PrettyPrintRecursive(sb, indentation + 1, value.GetType() != param.Property.PropertyType);
                }
            }
            if (indentation > 0)
            {
                sb.AppendTabs(Math.Max(0, indentation - 1)).AppendLine("}");
            }
        }