本文整理汇总了C#中BsonReader.ReadBinaryData方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.ReadBinaryData方法的具体用法?C# BsonReader.ReadBinaryData怎么用?C# BsonReader.ReadBinaryData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonReader
的用法示例。
在下文中一共展示了BsonReader.ReadBinaryData方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(BsonBinaryData));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Binary:
return bsonReader.ReadBinaryData();
default:
var message = string.Format("Cannot deserialize BsonBinaryData from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例2: 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="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(Guid));
var bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Binary:
var binaryData = bsonReader.ReadBinaryData();
var bytes = binaryData.Bytes;
var subType = binaryData.SubType;
var guidRepresentation = binaryData.GuidRepresentation;
if (bytes.Length != 16)
{
message = string.Format("Expected length to be 16, not {0}.", bytes.Length);
throw new FileFormatException(message);
}
if (subType != BsonBinarySubType.UuidStandard && subType != BsonBinarySubType.UuidLegacy)
{
message = string.Format("Expected binary sub type to be UuidStandard or UuidLegacy, not {0}.", subType);
throw new FileFormatException(message);
}
if (guidRepresentation == GuidRepresentation.Unspecified)
{
throw new BsonSerializationException("GuidSerializer cannot deserialize a Guid when GuidRepresentation is Unspecified.");
}
return GuidConverter.FromBytes(bytes, guidRepresentation);
case BsonType.String:
return new Guid(bsonReader.ReadString());
default:
message = string.Format("Cannot deserialize Guid from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例3: Deserialize
// public methods
/// <summary>
/// Deserializes an Bitmap from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the Bitmap.</param>
/// <param name="actualType">The actual type of the Bitmap.</param>
/// <param name="options">The serialization options.</param>
/// <returns>A Bitmap.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
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 (actualType != typeof(Bitmap))
{
var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName);
throw new ArgumentException(message, "actualType");
}
var bsonType = bsonReader.GetCurrentBsonType();
byte[] bytes;
BsonBinarySubType subType;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
bsonReader.ReadBinaryData(out bytes, out subType);
break;
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadString("_t");
bsonReader.ReadBinaryData("bitmap", out bytes, out subType);
bsonReader.ReadEndDocument();
break;
default:
var message = string.Format("BsonType must be Null, Binary or Document, not {0}.", bsonType);
throw new FileFormatException(message);
}
if (subType != BsonBinarySubType.Binary)
{
var message = string.Format("Binary sub type must be Binary, not {0}.", subType);
throw new FileFormatException(message);
}
var stream = new MemoryStream(bytes);
return new Bitmap(stream);
}
示例4: Deserialize
// public methods
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
/// <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="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(BitArray));
BsonType bsonType = bsonReader.GetCurrentBsonType();
BitArray bitArray;
byte[] bytes;
BsonBinarySubType subType;
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
bsonReader.ReadBinaryData(out bytes, out subType);
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary)
{
message = string.Format("Invalid Binary sub type {0}.", subType);
throw new FileFormatException(message);
}
return new BitArray(bytes);
case BsonType.Document:
bsonReader.ReadStartDocument();
var length = bsonReader.ReadInt32("Length");
bsonReader.ReadBinaryData("Bytes", out bytes, out subType);
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary)
{
message = string.Format("Invalid Binary sub type {0}.", subType);
throw new FileFormatException(message);
}
bsonReader.ReadEndDocument();
bitArray = new BitArray(bytes);
bitArray.Length = length;
return bitArray;
case BsonType.String:
var s = bsonReader.ReadString();
bitArray = new BitArray(s.Length);
for (int i = 0; i < s.Length; i++)
{
var c = s[i];
switch (c)
{
case '0':
break;
case '1':
bitArray[i] = true;
break;
default:
throw new FileFormatException("String value is not a valid BitArray.");
}
}
return bitArray;
default:
message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例5: 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;
}
示例6: 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="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(BsonBinaryData));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
byte[] bytes;
BsonBinarySubType subType;
GuidRepresentation guidRepresentation;
bsonReader.ReadBinaryData(out bytes, out subType, out guidRepresentation);
return new BsonBinaryData(bytes, subType, guidRepresentation);
default:
var message = string.Format("Cannot deserialize BsonBinaryData from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}