Code snippets from my projects, ready to use; tiny tests; code examples.
using System;
using System.Collections.Generic;
namespace Helper
{
public static class ObjectExtensions
{
public static IEnumerable<string> ObjectProperties(this object obj)
{
var dump = new List<string>();
var properties = obj.GetType().GetProperties();
foreach (var prop in properties)
{
if (prop.GetValue(obj, null) is ICollection collectionItems)
{
dump.Add(prop.Name);
foreach (var item in collectionItems)
{
dump.Add($" - {item}");
var objectType = item.GetType();
foreach (var nestedObjectProp in objectType.GetProperties())
{
dump.Add($" --- {nestedObjectProp.Name}: {nestedObjectProp.GetValue(item)}");
}
}
}
else if (prop.GetValue(obj, null) is Object nestedObject)
{
dump.Add($"{prop.Name}: {prop.GetValue(obj, null)}");
var nestedType = nestedObject.GetType();
foreach (var nestedTypeProp in nestedType.GetProperties())
{
dump.Add($" --- {nestedTypeProp.Name}: {nestedTypeProp.GetValue(nestedObject)}");
}
}
}
return dump;
}
}
}