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


C# IBsonReader.ReadBinaryData方法代码示例

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


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

示例1: TestGuid

 public void TestGuid()
 {
     var guid = new Guid("B5F21E0C2A0D42D6AD03D827008D8AB6");
     var json = "CSUUID(\"B5F21E0C2A0D42D6AD03D827008D8AB6\")";
     using (_bsonReader = new JsonReader(json))
     {
         Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType());
         var binaryData = _bsonReader.ReadBinaryData();
         Assert.True(binaryData.Bytes.SequenceEqual(guid.ToByteArray()));
         Assert.Equal(BsonBinarySubType.UuidLegacy, binaryData.SubType);
         Assert.Equal(GuidRepresentation.CSharpLegacy, binaryData.GuidRepresentation);
         Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
     }
     var expected = "CSUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")";
     Assert.Equal(expected, BsonSerializer.Deserialize<Guid>(json).ToJson());
 }
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:16,代码来源:JsonReaderTests.cs

示例2: GetActualType

        // public methods
        /// <summary>
        /// Gets the actual type of an object by reading the discriminator from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The reader.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <returns>The actual type.</returns>
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            // the BsonReader is sitting at the value whose actual type needs to be found
            var bsonType = bsonReader.GetCurrentBsonType();
            if (bsonReader.State == BsonReaderState.Value)
            {
                Type primitiveType = null;
                switch (bsonType)
                {
                    case BsonType.Boolean: primitiveType = typeof(bool); break;
                    case BsonType.Binary:
                        var bookmark = bsonReader.GetBookmark();
                        var binaryData = bsonReader.ReadBinaryData();
                        var subType = binaryData.SubType;
                        if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
                        {
                            primitiveType = typeof(Guid);
                        }
                        bsonReader.ReturnToBookmark(bookmark);
                        break;
                    case BsonType.DateTime: primitiveType = typeof(DateTime); break;
                    case BsonType.Decimal128: primitiveType = typeof(Decimal128); break;
                    case BsonType.Double: primitiveType = typeof(double); break;
                    case BsonType.Int32: primitiveType = typeof(int); break;
                    case BsonType.Int64: primitiveType = typeof(long); break;
                    case BsonType.ObjectId: primitiveType = typeof(ObjectId); break;
                    case BsonType.String: primitiveType = typeof(string); break;
                }

                // Type.IsAssignableFrom is extremely expensive, always perform a direct type check before calling Type.IsAssignableFrom
                if (primitiveType != null && (primitiveType == nominalType || nominalType.GetTypeInfo().IsAssignableFrom(primitiveType)))
                {
                    return primitiveType;
                }
            }

            if (bsonType == BsonType.Document)
            {
                var bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();
                var actualType = nominalType;
                if (bsonReader.FindElement(_elementName))
                {
                    var context = BsonDeserializationContext.CreateRoot(bsonReader);
                    var discriminator = BsonValueSerializer.Instance.Deserialize(context);
                    if (discriminator.IsBsonArray)
                    {
                        discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
                    }
                    actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
                }
                bsonReader.ReturnToBookmark(bookmark);
                return actualType;
            }

            return nominalType;
        }
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:64,代码来源:ObjectDiscriminatorConvention.cs


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