Enumerable Debugger

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bw.Sipp
{
    public static class EnumerableDebugger
    {
        public static IEnumerable<T> Dump<T>(this IEnumerable<T> input)
        {
            return Dump(
                input,
                item => item != null ? Newtonsoft.Json.JsonConvert.SerializeObject(item) : "(null)");
        }

        public static IEnumerable<T> Dump<T>(
            this IEnumerable<T> input,
            Func<T, string> toString)
        {
            foreach (var item in input)
            {
                Debug.WriteLine(toString(item));
                yield return item;
            }
        }
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.