using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
public static void Main()
var ListToSend = new List<List<string>>();
var ListToReceive = new List<List<string>>();
ListToSend = SimulateData().ToList();
using (var stream = GetStream(ListToSend))
var formatter = new BinaryFormatter();
List<string> row = formatter.Deserialize(stream) as List<string>;
Console.WriteLine("Done");
private static void Printer(List<List<string>> data)
Console.WriteLine("Printing");
foreach (var row in data)
foreach (var cell in row)
Console.Write(cell + "\t");
Console.WriteLine("-------------------------------------------------------------------------------");
private static EnumerableStream<T> GetStream<T>(IEnumerable<T> data)
return EnumerableStream<T>.Create(data, DeserializerCallback);
private static List<byte> DeserializerCallback<T>(T obj)
var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, obj);
return mStream.ToArray().ToList();
private static IEnumerable<List<string>> SimulateData()
Random randomizer = new Random();
for (var i = 0; i < 10; i++)
var row = new List<string>();
for (var j = 0; j < 1000; j++)
row.Add((randomizer.Next(100)).ToString());
public class EnumerableStream<T> : Stream
private readonly IEnumerator<T> _source;
private readonly Func<T, IEnumerable<byte>> _serializer;
private readonly Queue<byte> _buf = new Queue<byte>();
public bool HasMore { get { return _buf.Any() || SerializeNext();} }
public override bool CanRead { get {return true;} }
public override bool CanSeek { get {return false;} }
public override bool CanWrite { get {return false;} }
public override long Length { get {return -1;} }
public EnumerableStream(IEnumerable<T> source, Func<T, IEnumerable<byte>> serializer)
_source = source.GetEnumerator();
_serializer = serializer;
public static EnumerableStream<T> Create(IEnumerable<T> source, Func<T, List<byte>> serializer)
return new EnumerableStream<T>(source, serializer);
private bool SerializeNext()
foreach (var b in _serializer(_source.Current))
if (_buf.Any() || SerializeNext())
public override int Read(byte[] buffer, int offset, int count)
buffer[offset + read] = (byte)mayb;
public override void Write(byte[] buffer, int offset, int count)
throw new NotSupportedException();
public override void Flush()
throw new NotSupportedException();
public override long Seek(long offset, SeekOrigin origin)
throw new NotSupportedException();
public override void SetLength(long value)
throw new NotSupportedException();
public override long Position
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }