Skip to content

Instantly share code, notes, and snippets.

@whyleee
Last active Oct 14, 2015
Embed
What would you like to do?
Function to write RDB C# objects to console
private static void WriteObject(object obj, int indent = 0, string suffix = null)
{
foreach (var prop in obj.GetType().GetProperties())
{
var value = prop.GetValue(obj);
if (prop.PropertyType.FullName.StartsWith("RDB") && !prop.PropertyType.IsEnum && value != null)
{
Console.Write(new string(' ', indent));
Console.WriteLine(prop.Name + ":");
WriteObject(value, indent + 2);
}
else if (typeof (IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string))
{
Console.Write(new string(' ', indent));
Console.WriteLine(prop.Name + ": [");
foreach (var item in (IEnumerable) value)
{
if (item.GetType().FullName.StartsWith("RDB") && !item.GetType().IsEnum)
{
WriteObject(item, indent + 2);
}
else
{
Console.Write(new string(' ', indent + 2));
Console.WriteLine(item);
}
}
Console.Write(new string(' ', indent));
Console.WriteLine("]");
}
else
{
Console.Write(new string(' ', indent));
Console.WriteLine(prop.Name + ": " + value + suffix);
}
}
if (indent == 0)
{
Console.WriteLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment