Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class CollectionPrettyString
- {
- public static string ToPrettyString(this IEnumerable collection)
- {
- if (collection == null) return "null";
- var builder = new StringBuilder();
- var index = 0;
- builder.Append("[ ");
- foreach (var item in collection)
- {
- if (index > 0) builder.Append(", ");
- if (IsValidCollection(item))
- builder.Append(ToPrettyString(item as IEnumerable));
- else
- builder.Append(item.ToString());
- index++;
- }
- builder.Append(" ]");
- return builder.ToString();
- }
- public static bool IsValidCollection(object instance)
- {
- if (instance.GetType() == typeof(string)) return false;
- return instance is IEnumerable;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement