Use protobuf.net, find here , it is much faster!
Update
From a look at your code, I assume there is no re-evaluation so that the computed hashes are consistent between AppDomains? If you are not calculating your HashCode, this might be simple:
private static long GenerateHash(object key) { long typeHash = key.GetType().GetHashCode(); long keyHash = key.GetHashCode(); return (typeHash << 32) + keyHash; }
For future use, your MemoryStream must really be in the block you are using, otherwise you risk a memory leak:
private byte[] ObjectToByteArray(Object obj) { if (obj == null) return null; using (MemoryStream ms = new MemoryStream()) { binForm.Serialize(ms, obj); return ms.ToArray(); } }
source share