public void WriteObject()

in sdk/SdkSamples/Helpers/ConsoleHelper.cs [213:291]


        public void WriteObject(object @object, string title = default(string), int indent = 0)
        {
            if (@object == null)
            {
                return;
            }

            const int TabSize = 4;
            bool isTitlePresent = !string.IsNullOrWhiteSpace(title);
            string indentString = new string(' ', indent * TabSize);
            Type objectType = @object.GetType();
            var collection = @object as ICollection;

            if (objectType.Assembly.FullName == typeof(ResourceBase).Assembly.FullName && objectType.IsClass)
            {
                // this is a partner SDK model class, iterate over it's properties recursively
                if (indent == 0 && !string.IsNullOrWhiteSpace(title))
                {
                    Console.WriteLine(title);
                    Console.WriteLine(new string('-', 90));
                }
                else if (isTitlePresent)
                {
                    this.WriteColored(string.Format(CultureInfo.InvariantCulture, "{0}{1}: ", indentString, title), ConsoleColor.Yellow);
                }
                else
                {
                    // since the current element does not have a title, we do not want to shift it's children causing a double shift appearance
                    // to the user
                    indent--;
                }

                PropertyInfo[] properties = objectType.GetProperties();

                foreach (PropertyInfo property in properties)
                {
                    if (property.PropertyType.IsArray)
                    {
                        IEnumerable<object> objectList = (IEnumerable<object>)property.GetValue(@object);
                        foreach (object obj in objectList)
                        {
                            this.WriteObject(obj, property.Name, indent + 1);
                        }

                    }
                    else
                    {
                        this.WriteObject(property.GetValue(@object), property.Name, indent + 1);
                    }
                }

                if (indent == 0 && !string.IsNullOrWhiteSpace(title))
                {
                    Console.WriteLine(new string('-', 90));
                }
            }
            else if (collection != null)
            {
                // this is a collection, loop through its element and print them recursively
                this.WriteColored(string.Format(CultureInfo.InvariantCulture, isTitlePresent ? "{0}{1}: " : string.Empty, indentString, title), ConsoleColor.Yellow);

                foreach (var element in collection)
                {
                    this.WriteObject(element, indent: indent + 1);
                    var elementType = element.GetType();

                    if (indent == 1)
                    {
                        Console.WriteLine(new string('-', 80));
                    }
                }
            }
            else
            {
                // print the object as is
                this.WriteColored(string.Format(CultureInfo.InvariantCulture, isTitlePresent ? "{0}{1}: " : "{0}", indentString, title), ConsoleColor.DarkYellow, false);
                Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}", @object));
            }
        }