當前位置: 首頁>>代碼示例>>C#>>正文


C# BsonWriter.WriteBinaryData方法代碼示例

本文整理匯總了C#中MongoDB.Bson.IO.BsonWriter.WriteBinaryData方法的典型用法代碼示例。如果您正苦於以下問題:C# BsonWriter.WriteBinaryData方法的具體用法?C# BsonWriter.WriteBinaryData怎麽用?C# BsonWriter.WriteBinaryData使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在MongoDB.Bson.IO.BsonWriter的用法示例。


在下文中一共展示了BsonWriter.WriteBinaryData方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Serialize

        #pragma warning restore 618

        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            bool serializeIdFirst
        ) {
            if (value == null) {
                bsonWriter.WriteNull();
            } else {
                bsonWriter.WriteBinaryData((byte[]) value, BsonBinarySubType.Binary);
            }
        }
開發者ID:testn,項目名稱:mongo-csharp-driver,代碼行數:14,代碼來源:NetPrimitiveSerializers.cs

示例2: Serialize

        public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            var underlyingValue = value.GetType().GetProperty("Value").GetValue(value, null);
            var underlyingValueType = nominalType.GetConceptValueType();
			if (underlyingValueType == typeof(Guid)) {
				var guid = (Guid)underlyingValue;
				var guidAsBytes = guid.ToByteArray ();
				bsonWriter.WriteBinaryData (guidAsBytes, BsonBinarySubType.UuidLegacy, GuidRepresentation.CSharpLegacy);
			} else if (underlyingValueType == typeof(double))
				bsonWriter.WriteDouble ((double)underlyingValue);
			else if (underlyingValueType == typeof(float))
				bsonWriter.WriteDouble ((double)underlyingValue);
			else if (underlyingValueType == typeof(Int32))
				bsonWriter.WriteInt32 ((Int32)underlyingValue);
			else if (underlyingValueType == typeof(Int64))
				bsonWriter.WriteInt64 ((Int64)underlyingValue);
			else if (underlyingValueType == typeof(bool))
				bsonWriter.WriteBoolean ((bool)underlyingValue);
			else if (underlyingValueType == typeof(string))
				bsonWriter.WriteString ((string)(underlyingValue ?? string.Empty));
			else if (underlyingValueType == typeof(decimal))
				bsonWriter.WriteString (underlyingValue.ToString());
        }
開發者ID:jarlef,項目名稱:Bifrost,代碼行數:23,代碼來源:ConceptSerializer.cs

示例3: Serialize

        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            var guid = (Guid)value;
            var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);

            switch (representationSerializationOptions.Representation)
            {
                case BsonType.Binary:
                    var writerGuidRepresentation = bsonWriter.Settings.GuidRepresentation;
                    if (writerGuidRepresentation == GuidRepresentation.Unspecified)
                    {
                        throw new BsonSerializationException("GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified.");
                    }
                    var bytes = GuidConverter.ToBytes(guid, writerGuidRepresentation);
                    var subType = (writerGuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
                    bsonWriter.WriteBinaryData(bytes, subType, writerGuidRepresentation);
                    break;
                case BsonType.String:
                    bsonWriter.WriteString(guid.ToString());
                    break;
                default:
                    var message = string.Format("'{0}' is not a valid Guid representation.", representationSerializationOptions.Representation);
                    throw new BsonSerializationException(message);
            }
        }
開發者ID:ncipollina,項目名稱:mongo-csharp-driver,代碼行數:36,代碼來源:BsonPrimitiveSerializers.cs

示例4: Serialize

#pragma warning restore 618

        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var bitArray = (BitArray)value;
                var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);

                switch (representationSerializationOptions.Representation)
                {
                    case BsonType.Binary:
                        if ((bitArray.Length % 8) == 0)
                        {
                            bsonWriter.WriteBinaryData(GetBytes(bitArray), BsonBinarySubType.Binary);
                        }
                        else
                        {
                            bsonWriter.WriteStartDocument();
                            bsonWriter.WriteInt32("Length", bitArray.Length);
                            bsonWriter.WriteBinaryData("Bytes", GetBytes(bitArray), BsonBinarySubType.Binary);
                            bsonWriter.WriteEndDocument();
                        }
                        break;
                    case BsonType.String:
                        var sb = new StringBuilder(bitArray.Length);
                        for (int i = 0; i < bitArray.Length; i++)
                        {
                            sb.Append(bitArray[i] ? '1' : '0');
                        }
                        bsonWriter.WriteString(sb.ToString());
                        break;
                    default:
                        var message = string.Format("'{0}' is not a valid BitArray representation.", representationSerializationOptions.Representation);
                        throw new BsonSerializationException(message);
                }
            }
        }
開發者ID:davidxchen,項目名稱:mongo-csharp-driver,代碼行數:53,代碼來源:BitArraySerializer.cs

示例5: Serialize

 /// <summary>
 /// Serializes an object to a BsonWriter.
 /// </summary>
 /// <param name="bsonWriter">The BsonWriter.</param>
 /// <param name="nominalType">The nominal type.</param>
 /// <param name="value">The object.</param>
 /// <param name="options">The serialization options.</param>
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     IBsonSerializationOptions options
 ) {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         var binaryData = (BsonBinaryData) value;
         var bytes = binaryData.Bytes;
         var subType = binaryData.SubType;
         var guidRepresentation = binaryData.GuidRepresentation;
         if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) {
             var writerGuidRepresentation = bsonWriter.Settings.GuidRepresentation;
             if (writerGuidRepresentation != GuidRepresentation.Unspecified) {
                 if (guidRepresentation == GuidRepresentation.Unspecified) {
                     var message = string.Format("Cannot serialize BsonBinaryData with GuidRepresentation Unspecified to destination with GuidRepresentation {1}.", writerGuidRepresentation);
                     throw new BsonSerializationException(message);
                 }
                 if (guidRepresentation != writerGuidRepresentation) {
                     var guid = GuidConverter.FromBytes(bytes, guidRepresentation);
                     bytes = GuidConverter.ToBytes(guid, writerGuidRepresentation);
                     subType = (writerGuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
                     guidRepresentation = writerGuidRepresentation;
                 }
             }
         }
         bsonWriter.WriteBinaryData(bytes, subType, guidRepresentation);
     }
 }
開發者ID:redforks,項目名稱:mongo-csharp-driver,代碼行數:38,代碼來源:BsonValueSerializers.cs

示例6: Serialize

        /// <summary>
        /// Serializes a Bitmap to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The Bitmap.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (nominalType != typeof(Image) && nominalType != typeof(Bitmap))
            {
                var message = string.Format("Nominal type must be Image or Bitmap, not {0}.", nominalType.FullName);
                throw new ArgumentException(message, "nominalType");
            }

            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var actualType = value.GetType();
                if (actualType != typeof(Bitmap))
                {
                    var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName);
                    throw new ArgumentException(message, "actualType");
                }

                var bitmap = (Bitmap)value;
                var stream = new MemoryStream();
                bitmap.Save(stream, ImageFormat.Bmp);
                var bytes = stream.ToArray();

                if (nominalType == typeof(Image))
                {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteString("_t", "Bitmap");
                    bsonWriter.WriteBinaryData("bitmap", bytes, BsonBinarySubType.Binary);
                    bsonWriter.WriteEndDocument();
                }
                else
                {
                    bsonWriter.WriteBinaryData(bytes, BsonBinarySubType.Binary);
                }
            }
        }
開發者ID:davidxchen,項目名稱:mongo-csharp-driver,代碼行數:50,代碼來源:BitmapSerializer.cs

示例7: Serialize

        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            var byteValue = (byte)value;
            var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);

            switch (representationSerializationOptions.Representation)
            {
                case BsonType.Binary:
                    bsonWriter.WriteBinaryData(new byte[] { byteValue }, BsonBinarySubType.Binary);
                    break;
                case BsonType.Int32:
                    bsonWriter.WriteInt32(byteValue);
                    break;
                case BsonType.Int64:
                    bsonWriter.WriteInt64(byteValue);
                    break;
                case BsonType.String:
                    bsonWriter.WriteString(string.Format("{0:x2}", byteValue));
                    break;
                default:
                    var message = string.Format("'{0}' is not a valid Byte representation.", representationSerializationOptions.Representation);
                    throw new BsonSerializationException(message);
            }
        }
開發者ID:abel,項目名稱:sinan,代碼行數:35,代碼來源:NetPrimitiveSerializers.cs

示例8: Serialize

 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 )
 {
     var guid = (Guid) value;
     bsonWriter.WriteBinaryData(guid.ToByteArray(), BsonBinarySubType.Uuid);
 }
開發者ID:tomthink,項目名稱:mongo-csharp-driver,代碼行數:10,代碼來源:BsonPrimitiveSerializers.cs

示例9: Serialize

 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     IBsonSerializationOptions options
 ) {
     var sbyteValue = (sbyte) value;
     var representation = (options == null) ? BsonType.Int32 : ((RepresentationSerializationOptions) options).Representation;
     switch (representation) {
         case BsonType.Binary:
             bsonWriter.WriteBinaryData(new byte[] { (byte) sbyteValue }, BsonBinarySubType.Binary);
             break;
         case BsonType.Int32:
             bsonWriter.WriteInt32(sbyteValue);
             break;
         case BsonType.Int64:
             bsonWriter.WriteInt64(sbyteValue);
             break;
         case BsonType.String:
             bsonWriter.WriteString(string.Format("{0:x2}", sbyteValue));
             break;
         default:
             var message = string.Format("'{0}' is not a valid representation for type 'Byte'", representation);
             throw new BsonSerializationException(message);
     }
 }
開發者ID:daniaos,項目名稱:mongo-csharp-driver,代碼行數:26,代碼來源:NetPrimitiveSerializers.cs

示例10: Serialize

 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     IBsonSerializationOptions options
 )
 {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         var binaryData = (BsonBinaryData) value;
         bsonWriter.WriteBinaryData(binaryData.Bytes, binaryData.SubType);
     }
 }
開發者ID:kolupaev,項目名稱:mongo-csharp-driver,代碼行數:14,代碼來源:BsonValueSerializers.cs

示例11: Serialize

        #pragma warning restore 618

        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            bool serializeIdFirst
        ) {
            if (value == null) {
                bsonWriter.WriteNull();
            } else {
                var bitArray = (BitArray) value;
                if ((bitArray.Length % 8) == 0) {
                    bsonWriter.WriteBinaryData(GetBytes(bitArray), BsonBinarySubType.Binary);
                } else {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteInt32("Length", bitArray.Length);
                    bsonWriter.WriteBinaryData("Bytes", GetBytes(bitArray), BsonBinarySubType.Binary);
                    bsonWriter.WriteEndDocument();
                }
            }
        }
開發者ID:kenegozi,項目名稱:mongo-csharp-driver,代碼行數:22,代碼來源:NetPrimitiveSerializers.cs

示例12: if

 void IBsonSerializable.Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     IBsonSerializationOptions options
 )
 {
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteString("$ref", collectionName);
     if (id is ObjectId) {
         var objectId = (ObjectId) id;
         bsonWriter.WriteObjectId("$id", objectId.Timestamp, objectId.Machine, objectId.Pid, objectId.Increment);
     } else if (id is Guid) {
         var guid = (Guid) id;
         bsonWriter.WriteBinaryData("$id", guid.ToByteArray(), BsonBinarySubType.Uuid);
     } else if (id is int) {
         bsonWriter.WriteInt32("$id", (int) id);
     } else if (id is long) {
         bsonWriter.WriteInt64("$id", (long) id);
     } else if (id is string) {
         bsonWriter.WriteString("$id", (string) id);
     } else {
         var message = string.Format("Unexpected Id type: {0}", id.GetType().FullName);
         throw new BsonInternalException(message);
     }
     if (databaseName != null) {
         bsonWriter.WriteString("$db", databaseName);
     }
     bsonWriter.WriteEndDocument();
 }
開發者ID:jenrom,項目名稱:mongo-csharp-driver,代碼行數:29,代碼來源:MongoDBRef.cs

示例13: Serialize

        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options
        ) {
            var guid = (Guid) value;
            var guidSerializationOptions = options as GuidSerializationOptions;
            if (guidSerializationOptions == null) {
                var representationOptions = options as RepresentationSerializationOptions;
                if (representationOptions != null) {
                    guidSerializationOptions = new GuidSerializationOptions(representationOptions.Representation);
                }
            }
            if (guidSerializationOptions == null) {
                guidSerializationOptions = GuidSerializationOptions.Defaults;
            }

            switch (guidSerializationOptions.Representation) {
                case BsonType.Binary:
                    var bytes = guid.ToByteArray();
                    if (guidSerializationOptions.ByteOrder == GuidByteOrder.BigEndian && BitConverter.IsLittleEndian) {
                        bytes = (byte[]) bytes.Clone(); // Clone is defensive in case Guid.ToByteArray returns an internal copy
                        Array.Reverse(bytes, 0, 4);
                        Array.Reverse(bytes, 4, 2);
                        Array.Reverse(bytes, 6, 2);
                    }
                    bsonWriter.WriteBinaryData(bytes, BsonBinarySubType.Uuid);
                    break;
                case BsonType.String:
                    bsonWriter.WriteString(guid.ToString());
                    break;
                default:
                    var message = string.Format("'{0}' is not a valid representation for type 'Guid'", guidSerializationOptions.Representation);
                    throw new BsonSerializationException(message);
            }
        }
開發者ID:oskysal,項目名稱:mongo-csharp-driver,代碼行數:44,代碼來源:BsonPrimitiveSerializers.cs

示例14: Serialize

 /// <summary>
 /// Serializes an object to a BsonWriter.
 /// </summary>
 /// <param name="bsonWriter">The BsonWriter.</param>
 /// <param name="nominalType">The nominal type.</param>
 /// <param name="value">The object.</param>
 /// <param name="options">The serialization options.</param>
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     IBsonSerializationOptions options
 ) {
     var guid = (Guid) value;
     var representation = (options == null) ? BsonType.Binary : ((RepresentationSerializationOptions) options).Representation;
     switch (representation) {
         case BsonType.Binary:
             bsonWriter.WriteBinaryData(guid.ToByteArray(), BsonBinarySubType.Uuid);
             break;
         case BsonType.String:
             bsonWriter.WriteString(guid.ToString());
             break;
         default:
             var message = string.Format("'{0}' is not a valid representation for type 'Guid'", representation);
             throw new BsonSerializationException(message);
     }
 }
開發者ID:ebix,項目名稱:mongo-csharp-driver,代碼行數:27,代碼來源:BsonPrimitiveSerializers.cs

示例15: Serialize

 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 )
 {
     var guid = (Guid) value;
     switch (representation) {
         case BsonType.Binary:
             bsonWriter.WriteBinaryData(guid.ToByteArray(), BsonBinarySubType.Uuid);
             break;
         case BsonType.String:
             bsonWriter.WriteString(guid.ToString());
             break;
         default:
             throw new BsonInternalException("Unexpected representation");
     }
 }
開發者ID:kenegozi,項目名稱:mongo-csharp-driver,代碼行數:19,代碼來源:BsonPrimitiveSerializers.cs


注:本文中的MongoDB.Bson.IO.BsonWriter.WriteBinaryData方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。