本文整理汇总了C#中BsonReader.GetBookmark方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.GetBookmark方法的具体用法?C# BsonReader.GetBookmark怎么用?C# BsonReader.GetBookmark使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonReader
的用法示例。
在下文中一共展示了BsonReader.GetBookmark方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{
if (nominalType != typeof(object))
{
var message = string.Format("ObjectSerializer can only be used with nominal type System.Object, not type {0}.", nominalType.FullName);
throw new InvalidOperationException(message);
}
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else if (bsonType == BsonType.Document)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
if (bsonReader.ReadBsonType() == BsonType.EndOfDocument)
{
bsonReader.ReadEndDocument();
return new object();
}
else
{
bsonReader.ReturnToBookmark(bookmark);
}
}
var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object));
var actualType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
if (actualType == typeof(object))
{
var message = string.Format("Unable to determine actual type of object to deserialize. NominalType is System.Object and BsonType is {0}.", bsonType);
throw new FileFormatException(message);
}
var serializer = BsonSerializer.LookupSerializer(actualType);
return serializer.Deserialize(bsonReader, nominalType, actualType, options);
}
示例2: IsCSharpNullRepresentation
// private methods
private bool IsCSharpNullRepresentation(BsonReader bsonReader)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.Boolean)
{
var name = bsonReader.ReadName();
if (name == "_csharpnull" || name == "$csharpnull")
{
var value = bsonReader.ReadBoolean();
if (value)
{
bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.EndOfDocument)
{
bsonReader.ReadEndDocument();
return true;
}
}
}
}
bsonReader.ReturnToBookmark(bookmark);
return false;
}
示例3: 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(BsonReader 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();
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
{
primitiveType = typeof(Guid);
}
bsonReader.ReturnToBookmark(bookmark);
break;
case BsonType.DateTime: primitiveType = typeof(DateTime); 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.IsAssignableFrom(primitiveType)))
{
return primitiveType;
}
}
if (bsonType == BsonType.Document)
{
// ensure KnownTypes of nominalType are registered (so IsTypeDiscriminated returns correct answer)
BsonSerializer.EnsureKnownTypesAreRegistered(nominalType);
// we can skip looking for a discriminator if nominalType has no discriminated sub types
if (BsonSerializer.IsTypeDiscriminated(nominalType))
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var actualType = nominalType;
if (bsonReader.FindElement(_elementName))
{
var discriminator = BsonValue.ReadFrom(bsonReader);
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;
}