public static TOut SerializeAndDeserializeBson <TIn, TOut>(this TIn value) { var obj = new ObjectHolder <TIn> { Value = value }; var stream = new MemoryStream(); using (var writer = new BsonBinaryWriter(stream)) { BsonSerializer.Serialize(writer, obj); writer.Flush(); } stream.Position = 0; using (var reader = new BsonBinaryReader(stream)) { var result = BsonSerializer.Deserialize <ObjectHolder <TOut> >(reader); return(result.Value); } }
public void TestFlushAndClose() { var stream = new MemoryStream(); using (var bsonWriter = new BsonBinaryWriter(stream)) { bsonWriter.WriteStartDocument(); bsonWriter.WriteEndDocument(); bsonWriter.Flush(); bsonWriter.Close(); } }
private static TOut SerializeAndDeserializeBson <TIn, TOut>(TIn source) { var stream = new MemoryStream(); using (var writer = new BsonBinaryWriter(stream)) { BsonSerializer.Serialize(writer, source); writer.Flush(); } stream.Position = 0; using (var reader = new BsonBinaryReader(stream)) { var target = BsonSerializer.Deserialize <TOut>(reader); return(target); } }