using System.Collections.Generic;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Globalization;
using System.ComponentModel;
public long Id { get; set; }
public override string ToString()
return string.Format("{0}: Id = {1}", base.ToString(), Id);
this.People = new ObservableCollection<Person>();
public ObservableCollection<Person> People { get; private set; }
public event EventHandler<EventArgs<StreamingContext>> OnDeserialized;
internal void OnDeserializedMethod(StreamingContext context)
var onDeserialized = OnDeserialized;
if (onDeserialized != null)
onDeserialized(this, new EventArgs<StreamingContext> { Value = context });
public class EventArgs<T> : EventArgs
public T Value { get; set; }
const int Length = 10001;
var root = new RootObject();
foreach (var person in Enumerable.Range(0, Length).Select(i => new Person { Id = i }))
stream = new MemoryStream();
Serializer.Serialize(stream, root);
var newRoot = new RootObject();
newRoot.People.CollectionChanged += (o, e) =>
var collection = (ICollection<Person>)o;
ProcessItems(collection, false);
newRoot.OnDeserialized += (o, e) =>
ProcessItems(((RootObject)o).People, true);
Serializer.Merge(stream, newRoot);
Console.WriteLine("Done");
const int ProcessIncrement = 50;
void ProcessItems(ICollection<Person> people, bool force)
if (people == null || people.Count == 0)
if (people.Count >= ProcessIncrement || force)
Console.WriteLine(string.Format("Processing {0} people. first = \"{1}\", last = \"{2}\", stream.Position = {3}",
people.Count, people.First(), people.Last(), stream.Position));
public static void Main()
Console.WriteLine("protobuf-net version: " + typeof(ProtoBuf.Serializer).Assembly.FullName + "\n");
ProtoBuf.Meta.RuntimeTypeModel.Default.AutoCompile = false;