/// <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());
     }
 }
예제 #2
0
 /// <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());
     }
 }
예제 #3
0
 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;
     }
 }