本文整理汇总了C#中IBsonReader.GetBookmark方法的典型用法代码示例。如果您正苦于以下问题:C# IBsonReader.GetBookmark方法的具体用法?C# IBsonReader.GetBookmark怎么用?C# IBsonReader.GetBookmark使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IBsonReader
的用法示例。
在下文中一共展示了IBsonReader.GetBookmark方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetActualType
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
{
if (nominalType == typeof(JobTask))
{
var ret = nominalType;
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
if (bsonReader.FindElement(ElementName))
{
var value = bsonReader.ReadString();
ret = Type.GetType(value);
if (ret == null)
throw new Exception("Could not find type " + value);
if (!ret.IsSubclassOf(typeof(JobTask)))
throw new Exception("Database type does not inherit from JobTask.");
}
bsonReader.ReturnToBookmark(bookmark);
return ret;
}
else
{
return nominalType;
}
}
示例2: GetActualType
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
string typeValue = string.Empty;
if (bsonReader.FindElement(ElementName))
typeValue = bsonReader.ReadString();
else
throw new NotSupportedException();
bsonReader.ReturnToBookmark(bookmark);
var retr = Type.GetType(typeValue) ?? Type.GetType("ThreeOneThree.Proxima.Core.Entities." + typeValue);
return retr;
}
示例3: GetActualType
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var actualType = nominalType;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
if (name == "OnlyInB")
{
actualType = typeof(B);
break;
}
else if (name == "OnlyInC")
{
actualType = typeof(C);
break;
}
bsonReader.SkipValue();
}
bsonReader.ReturnToBookmark(bookmark);
return actualType;
}
示例4: 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 (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 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;
}
示例5: TestBookmark
public void TestBookmark()
{
var json = "{ \"x\" : 1, \"y\" : 2 }";
using (_bsonReader = new JsonReader(json))
{
// do everything twice returning to bookmark in between
var bookmark = _bsonReader.GetBookmark();
Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
_bsonReader.ReturnToBookmark(bookmark);
Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
bookmark = _bsonReader.GetBookmark();
_bsonReader.ReadStartDocument();
_bsonReader.ReturnToBookmark(bookmark);
_bsonReader.ReadStartDocument();
bookmark = _bsonReader.GetBookmark();
Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
_bsonReader.ReturnToBookmark(bookmark);
Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
bookmark = _bsonReader.GetBookmark();
Assert.Equal("x", _bsonReader.ReadName());
_bsonReader.ReturnToBookmark(bookmark);
Assert.Equal("x", _bsonReader.ReadName());
bookmark = _bsonReader.GetBookmark();
Assert.Equal(1, _bsonReader.ReadInt32());
_bsonReader.ReturnToBookmark(bookmark);
Assert.Equal(1, _bsonReader.ReadInt32());
bookmark = _bsonReader.GetBookmark();
Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
_bsonReader.ReturnToBookmark(bookmark);
Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
bookmark = _bsonReader.GetBookmark();
Assert.Equal("y", _bsonReader.ReadName());
_bsonReader.ReturnToBookmark(bookmark);
Assert.Equal("y", _bsonReader.ReadName());
bookmark = _bsonReader.GetBookmark();
Assert.Equal(2, _bsonReader.ReadInt32());
_bsonReader.ReturnToBookmark(bookmark);
Assert.Equal(2, _bsonReader.ReadInt32());
bookmark = _bsonReader.GetBookmark();
Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
_bsonReader.ReturnToBookmark(bookmark);
Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
bookmark = _bsonReader.GetBookmark();
_bsonReader.ReadEndDocument();
_bsonReader.ReturnToBookmark(bookmark);
_bsonReader.ReadEndDocument();
Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
}
Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
}
示例6: 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;
}