/// <summary> /// Converts an object to a BSON document byte array. /// </summary> /// <param name="obj">The object.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="options">The serialization options.</param> /// <param name="settings">The BsonBinaryWriter settings.</param> /// <returns>A byte array.</returns> public static byte[] ToBson(this object obj, Type nominalType, IBsonSerializationOptions options, BsonBinaryWriterSettings settings) { using (var buffer = new BsonBuffer()) { using (var bsonWriter = BsonWriter.Create(buffer, settings)) { BsonSerializer.Serialize(bsonWriter, nominalType, obj, options); } return(buffer.ToByteArray()); } }
/// <summary> /// Converts an object to a BSON document byte array. /// </summary> /// <typeparam name="T">The nominal type of the object.</typeparam> /// <param name="obj">The object.</param> /// <param name="options">The serialization options.</param> /// <param name="settings">The BsonBinaryWriter settings.</param> /// <returns>A byte array.</returns> public static byte[] ToBson <T>( this T obj, IBsonSerializationOptions options, BsonBinaryWriterSettings settings ) { using (var buffer = new BsonBuffer()) { using (var bsonWriter = BsonWriter.Create(buffer, settings)) { BsonSerializer.Serialize <T>(bsonWriter, obj, options); } return(buffer.ToByteArray()); } }
public static byte[] Serialize <T>(this T obj) where T : Serializable { try { using (var buffer = new BsonBuffer()) { using (var writer = BsonWriter.Create(buffer)) { BsonSerializer.Serialize(writer, typeof(T), obj); } return(buffer.ToByteArray()); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw ex; } }