当前位置: 首页>>代码示例>>C#>>正文


C# BSONDocument.Set方法代码示例

本文整理汇总了C#中BSONDocument.Set方法的典型用法代码示例。如果您正苦于以下问题:C# BSONDocument.Set方法的具体用法?C# BSONDocument.Set怎么用?C# BSONDocument.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BSONDocument的用法示例。


在下文中一共展示了BSONDocument.Set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InferSchema

        public void InferSchema()
        {
            var doc = new BSONDocument();
            doc.Set( new BSONStringElement("FullName", "Alex Bobby") );
            doc.Set( new BSONInt32Element("Age", 123) );
            doc.Set( new BSONBooleanElement("IsGood", true) );

            var c = new RowConverter();

            var schema = c.InferSchemaFromBSONDocument(doc);

            Assert.AreEqual(3, schema.FieldCount);

            Assert.AreEqual(0, schema["FullName"].Order);
            Assert.AreEqual(1, schema["Age"].Order);
            Assert.AreEqual(2, schema["IsGood"].Order);

            Assert.AreEqual(typeof(object), schema["FullName"].NonNullableType);
            Assert.AreEqual(typeof(object), schema["Age"].NonNullableType);
            Assert.AreEqual(typeof(object), schema["IsGood"].NonNullableType);

            var row = new DynamicRow(schema);
            c.BSONDocumentToRow(doc, row, null);

            Assert.AreEqual("Alex Bobby", row[0]);
            Assert.AreEqual(123,          row[1]);
            Assert.AreEqual(true,         row[2]);

            Assert.AreEqual("Alex Bobby", row["FullName"]);
            Assert.AreEqual(123,          row["Age"]);
            Assert.AreEqual(true,         row["IsGood"]);
        }
开发者ID:vlapchenko,项目名称:nfx,代码行数:32,代码来源:RowConverterTest.cs

示例2: WriteBigIntegers

 public void WriteBigIntegers(Stream stream)
 {
     stream.Position = 0;
       var root = new BSONDocument();
       root.Set(new BSONInt32Element("intMin", int.MinValue));
       root.Set(new BSONInt32Element("intMax", int.MaxValue));
       root.Set(new BSONInt64Element("longMin", long.MinValue));
       root.Set(new BSONInt64Element("longMax", long.MaxValue));
       root.WriteAsBSON(stream);
 }
开发者ID:vlapchenko,项目名称:nfx,代码行数:10,代码来源:BSONTestForm.cs

示例3: CollectionDrop

        public void CollectionDrop()
        {
            using(var client= new MongoClient("My Test"))
              {
            var collection = client.DefaultLocalServer["db1"]["ToDrop"];
            var doc1 = new BSONDocument();

            doc1.Set( new BSONStringElement("_id", "id1"))
            .Set( new BSONStringElement("val", "My value"))
            .Set( new BSONInt32Element("age", 125));

            var r = collection.Insert(doc1);
            Assert.AreEqual(1, r.TotalDocumentsAffected);

            collection.Drop();
            Assert.IsTrue( collection.Disposed );
              }
        }
开发者ID:vlapchenko,项目名称:nfx,代码行数:18,代码来源:BasicConnectorFunctionality.cs

示例4: DoConvertCLRtoBSON

          /// <summary>
          /// override to perform the conversion. the data is never null here, and ref cycles a ruled out
          /// </summary>
          protected virtual BSONElement DoConvertCLRtoBSON(string name, object data, Type dataType, string targetName)
          {
            //1 Primitive/direct types
            Func<string, object, BSONElement> func;
            if (m_CLRtoBSON.TryGetValue(dataType, out func)) return func(name, data);

            //2 Enums
            if (dataType.IsEnum)
              return name != null ? new BSONStringElement(name, data.ToString()) : new BSONStringElement(data.ToString());

            //3 Complex Types
            if (data is Row)
              return this.RowToBSONDocumentElement((Row)data, targetName, name: name);

            //IDictionary //must be before IEnumerable
            if (data is IDictionary)
            {
              var dict = (IDictionary)data;
              var result = new BSONDocument();
              foreach( var key in dict.Keys)
              {
                var fldName = key.ToString();
                var el = ConvertCLRtoBSON(fldName, dict[key], targetName);
                result.Set(el);
              }
              return name != null ? new BSONDocumentElement(name, result) : new BSONDocumentElement(result);
            }

            //IEnumerable
            if (data is IEnumerable)
            {
              var list = (IEnumerable)data;
              List<BSONElement> elements = new List<BSONElement>();
              foreach( var obj in list)
              {
                var el = ConvertCLRtoBSON(null, obj, targetName);
                elements.Add(el);
              }
              var result = name != null ? new BSONArrayElement(name, elements.ToArray()) : new BSONArrayElement(elements.ToArray());
              return result;
            }


            throw new BSONException(StringConsts.CLR_BSON_CONVERSION_TYPE_NOT_SUPPORTED_ERROR.Args(dataType.FullName));
          }
开发者ID:huoxudong125,项目名称:nfx,代码行数:48,代码来源:RowConverter.cs

示例5: docFromMessage

                  private BSONDocument docFromMessage(Message msg)
                  {
                    var doc = new BSONDocument();

                    var rc = new RowConverter();

                    doc.Set(new BSONStringElement("Guid", msg.Guid.ToString("N")));
                    doc.Set(new BSONStringElement("RelatedTo", msg.RelatedTo.ToString("N")));
                    doc.Set(new BSONStringElement("Type", msg.Type.ToString()));
                    doc.Set(new BSONInt32Element("Source", msg.Source));
                    doc.Set(new BSONInt64Element("TimeStamp", msg.TimeStamp.Ticks));
                    doc.Set(new BSONStringElement("Host", msg.Host));
                    doc.Set(new BSONStringElement("From", msg.From));
                    doc.Set(new BSONStringElement("Topic", msg.Topic));
                    doc.Set(new BSONStringElement("Text", msg.Text));
                    doc.Set(new BSONStringElement("Parameters", msg.Parameters));
                    doc.Set(new BSONStringElement("Exception", msg.Exception.ToMessageWithType()));
                    doc.Set(new BSONInt32Element("ThreadID", msg.ThreadID));

                    return doc;
                  }
开发者ID:vlapchenko,项目名称:nfx,代码行数:21,代码来源:MongoDBLogMessageDataStore.cs

示例6: WriteStringInt32DoubleMixedArray

        public void WriteStringInt32DoubleMixedArray()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            var array = new BSONElement[] { new BSONStringElement("apple"), new BSONInt32Element(3), new BSONDoubleElement(2.14D) };
            root.Set(new BSONArrayElement("stuff", array));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 48); // ensure document length is 48 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(48));       // document's content length is 48
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array);                 // element type is array 0x04
            CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("stuff")); // element name is 'stuff'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                  // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(36));       // array's content length is 36

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String);               // element type is string 0x02
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0"));    // element name is '0'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                 // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(6));       // string content length is 6
            CollectionAssert.AreEqual(reader.ReadBytes(5),Encoding.UTF8.GetBytes("apple")); // string content length is 'apple'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                 // last byte is terminator 0x00

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);             // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(3));    // value is 3

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Double);             // element type is double 0x01
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(2.14D)); // value is 2.14

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 48); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:43,代码来源:BSON.cs

示例7: WriteStringAndInt32Pair

        public void WriteStringAndInt32Pair()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            root.Set(new BSONStringElement("name", "Gagarin"));
            root.Set(new BSONInt32Element("birth", 1934));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 34); // ensure document length is 38 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(34)); // ensure content length is 34
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // ensure element type is string 0x02
            CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("name")); // ensure element name is 'name'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(8)); // ensure string content length is 8
            CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("Gagarin")); // ensure element value is 'Gagarin'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // ensure element type is int 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("birth")); // ensure element name is 'birth'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1934)); // ensure element value is int 1934
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 34); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:31,代码来源:BSON.cs

示例8: WriteSingleString

        public void WriteSingleString()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            root.Set(new BSONStringElement("greetings", "Hello World!"));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 33); // ensure document length is 33 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(33)); // ensure content length is 33
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // ensure element type is string 0x02
            CollectionAssert.AreEqual(reader.ReadBytes(9), Encoding.UTF8.GetBytes("greetings")); // ensure element name is 'greetings'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(13)); // ensure string content length is 13
            CollectionAssert.AreEqual(reader.ReadBytes(12), Encoding.UTF8.GetBytes("Hello World!")); // ensure element value is 'Hello World!'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 33); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:25,代码来源:BSON.cs

示例9: WriteSingleObjectId

        public void WriteSingleObjectId()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var hex = "507f1f77bcf86cd799439011";
            var data = Enumerable.Range(0, hex.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
            var root = new BSONDocument();
            var objectId = new BSONObjectID(data);
            root.Set(new BSONObjectIDElement("objectId", objectId));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 27); // ensure document length is 27 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(27));          // content length is 27
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.ObjectID);                 // element type is objectID 0x07
            CollectionAssert.AreEqual(reader.ReadBytes(8), Encoding.UTF8.GetBytes("objectId")); // element name is 'objectId'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                     // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(12), data);                              // byte content is correct

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 27); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:30,代码来源:BSON.cs

示例10: WriteSingleBinaryData

        public void WriteSingleBinaryData()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var data = Encoding.UTF8.GetBytes("This is binary data");
            var root = new BSONDocument();
            var binary = new BSONBinary(BSONBinaryType.BinaryOld, data);
            root.Set(new BSONBinaryElement("binary", binary));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 37); // ensure document length is 37 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(37));        // content length is 37
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Binary);                 // element type is binary 0x05
            CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("binary")); // element name is 'binary'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                   // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(19));        // byte length is 19
            Assert.AreEqual(reader.ReadByte(), (byte)BSONBinaryType.BinaryOld);               // binary type is BinaryOld 0x02
            CollectionAssert.AreEqual(reader.ReadBytes(19), data);                            // byte content is correct

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 37); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:28,代码来源:BSON.cs

示例11: WriteReadStringsWithDifferentLengths

        public void WriteReadStringsWithDifferentLengths()
        {
            Parallel.For(0, 10*1024, i =>
              {
            using (var stream = new MemoryStream())
            using (var reader = new BinaryReader(stream))
            {
              // Write

              var root = new BSONDocument();
              var value = new string('a', i);
              root.Set(new BSONStringElement("vary", value));
              root.WriteAsBSON(stream);

              Assert.AreEqual(stream.Position, 16 + i); // ensure document length is 16+i bytes

              stream.Seek(0, SeekOrigin.Begin);

              CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(16 + i)); // content length is 16+i
              Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02
              CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("vary")); // element name is 'vary'
              Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present
              CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(i + 1)); // string content length is 13
              CollectionAssert.AreEqual(reader.ReadBytes(i), Encoding.UTF8.GetBytes(value)); // element value is value
              Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present
              Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // last byte is terminator 0x00

              Assert.AreEqual(stream.Position, 16 + i); // ensure whole document readed
              stream.Position = 0;

              // Read

              var deser = new BSONDocument(stream);

              Assert.AreEqual(deser.ByteSize, 16 + i);
              Assert.AreEqual(deser.Count, 1);

              var element = deser["vary"] as BSONStringElement;
              Assert.IsNotNull(element);
              Assert.AreEqual(element.ElementType, BSONElementType.String);
              Assert.AreEqual(element.Name, "vary");
              Assert.AreEqual(element.Value, value);
              Assert.AreEqual(stream.Position, 16 + i); // ensure whole document readed
              stream.Position = 0;
            }
              });
        }
开发者ID:itadapter,项目名称:nfx,代码行数:47,代码来源:BSON.cs

示例12: WriteReadSingleDateTime

        public void WriteReadSingleDateTime()
        {
            using (var stream = new MemoryStream())
              {
            var now = new DateTime(2015, 08, 26, 14, 23, 56);
            var bson1 = new BSONDocument();
            bson1.Set(new BSONDateTimeElement("now", now));
            bson1.WriteAsBSON(stream);

            stream.Position = 0;

            var bson2 = new BSONDocument(stream);

            var now1 = ((BSONDateTimeElement)bson1["now"]).Value;
            var now2 = ((BSONDateTimeElement)bson2["now"]).Value;

            Console.WriteLine("{0} {1}", now1, now1.Kind);
            Console.WriteLine("{0} {1}", now2, now2.Kind);

            Assert.AreEqual(now1.ToUniversalTime(), now2);
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:22,代码来源:BSON.cs

示例13: WriteNestedDocument

        public void WriteNestedDocument()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            var nested = new BSONDocument();
            nested.Set(new BSONStringElement("capital", "Moscow"));
            root.Set(new BSONDocumentElement("nested", nested));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 38); // ensure document length is 38 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(38)); // content length is 38
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Document);                  // element type is document 0x03
            CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("nested"));    // element name is 'nested'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                      // string name terminator 0x00 is present

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(25));           // nested document length is 25
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String);                    // element type is string 0x02
            CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("capital"));   // element name is 'capital'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                      // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(7));            // string length is 7
            CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("Moscow"));    // element value is 'Moscow'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 38); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:34,代码来源:BSON.cs

示例14: WriteInt32Array

        public void WriteInt32Array()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            var array = new[] { new BSONInt32Element(1963), new BSONInt32Element(1984), new BSONInt32Element(2015) };
            root.Set(new BSONArrayElement("years", array));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 38); // ensure document length is 38 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(38));       // document's content length is 38
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array);                 // element type is array 0x04
            CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("years")); // element name is 'years'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                  // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(26));       // array's content length is 26

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);             // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0")); // element name is '0'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1963)); // value is 1963

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);             // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1984)); // value is 1984

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);             // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(2015)); // value is 2015

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 38); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:41,代码来源:BSON.cs

示例15: WriteBigIntegers

        public void WriteBigIntegers()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            root.Set(new BSONInt32Element("intMin", int.MinValue));
            root.Set(new BSONInt32Element("intMax", int.MaxValue));
            root.Set(new BSONInt64Element("longMin", long.MinValue));
            root.Set(new BSONInt64Element("longMax", long.MaxValue));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 63); // ensure document length is 63 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(63));           // content length is 63

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);                     // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("intMin"));    // element name is 'intMin'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                      // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(int.MinValue)); // element value is int.MinValue

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);                     // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("intMax"));    // element name is 'intMax'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                      // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(int.MaxValue)); // element value is int.MaxValue

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64);                      // element type is int64 0x12
            CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("longMin"));    // element name is 'longMin'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                       // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(long.MinValue)); // element value is long.MinValue

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64);                      // element type is int64 0x12
            CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("longMax"));    // element name is 'longMax'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                       // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(long.MaxValue)); // element value is long.MaxValue

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 63); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:43,代码来源:BSON.cs


注:本文中的BSONDocument.Set方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。