Skip to content

Instantly share code, notes, and snippets.

@whyleee
Last active October 14, 2015 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whyleee/fc65391e61fe49e8eb94 to your computer and use it in GitHub Desktop.
Save whyleee/fc65391e61fe49e8eb94 to your computer and use it in GitHub Desktop.
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