本文整理汇总了C#中BsonReader.ReadInt32方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.ReadInt32方法的具体用法?C# BsonReader.ReadInt32怎么用?C# BsonReader.ReadInt32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonReader
的用法示例。
在下文中一共展示了BsonReader.ReadInt32方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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(DateTimeOffset));
BsonType bsonType = bsonReader.GetCurrentBsonType();
long ticks;
TimeSpan offset;
switch (bsonType)
{
case BsonType.Array:
bsonReader.ReadStartArray();
ticks = bsonReader.ReadInt64();
offset = TimeSpan.FromMinutes(bsonReader.ReadInt32());
bsonReader.ReadEndArray();
return new DateTimeOffset(ticks, offset);
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadDateTime("DateTime"); // ignore value
ticks = bsonReader.ReadInt64("Ticks");
offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset"));
bsonReader.ReadEndDocument();
return new DateTimeOffset(ticks, offset);
case BsonType.String:
return XmlConvert.ToDateTimeOffset(bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize DateTimeOffset 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(double));
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Double:
return bsonReader.ReadDouble();
case BsonType.Int32:
return representationSerializationOptions.ToDouble(bsonReader.ReadInt32());
case BsonType.Int64:
return representationSerializationOptions.ToDouble(bsonReader.ReadInt64());
case BsonType.String:
return XmlConvert.ToDouble(bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize Double from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例3: 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(bool));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Boolean:
return bsonReader.ReadBoolean();
case BsonType.Double:
return bsonReader.ReadDouble() != 0.0;
case BsonType.Int32:
return bsonReader.ReadInt32() != 0;
case BsonType.Int64:
return bsonReader.ReadInt64() != 0;
case BsonType.Null:
bsonReader.ReadNull();
return false;
case BsonType.String:
return XmlConvert.ToBoolean(bsonReader.ReadString().ToLower());
default:
var message = string.Format("Cannot deserialize Boolean from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例4: 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(decimal));
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Array:
var array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
var bits = new int[4];
bits[0] = array[0].AsInt32;
bits[1] = array[1].AsInt32;
bits[2] = array[2].AsInt32;
bits[3] = array[3].AsInt32;
return new decimal(bits);
case BsonType.Double:
return representationSerializationOptions.ToDecimal(bsonReader.ReadDouble());
case BsonType.Int32:
return representationSerializationOptions.ToDecimal(bsonReader.ReadInt32());
case BsonType.Int64:
return representationSerializationOptions.ToDecimal(bsonReader.ReadInt64());
case BsonType.String:
return XmlConvert.ToDecimal(bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize Decimal from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例5: Deserialize
// public methods
/// <summary>
/// Deserializes an object of type System.Drawing.Size 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(System.Drawing.Size));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Document:
bsonReader.ReadStartDocument();
var width = bsonReader.ReadInt32("Width");
var height = bsonReader.ReadInt32("Height");
bsonReader.ReadEndDocument();
return new System.Drawing.Size(width, height);
default:
var message = string.Format("Cannot deserialize Size from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例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(sbyte));
var bsonType = bsonReader.GetCurrentBsonType();
var lostData = false;
sbyte value;
switch (bsonType)
{
case BsonType.Binary:
var bytes = bsonReader.ReadBytes();
if (bytes.Length != 1)
{
throw new FileFormatException("Binary data for SByte must be exactly one byte long.");
}
value = (sbyte)bytes[0];
break;
case BsonType.Int32:
var int32Value = bsonReader.ReadInt32();
value = (sbyte)int32Value;
lostData = (int)value != int32Value;
break;
case BsonType.Int64:
var int64Value = bsonReader.ReadInt64();
value = (sbyte)int64Value;
lostData = (int)value != int64Value;
break;
case BsonType.String:
var s = bsonReader.ReadString();
if (s.Length == 1)
{
s = "0" + s;
}
value = (sbyte)byte.Parse(s, NumberStyles.HexNumber);
break;
default:
var message = string.Format("Cannot deserialize SByte from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
if (lostData)
{
var message = string.Format("Data loss occurred when trying to convert from {0} to SByte.", bsonType);
throw new FileFormatException(message);
}
return value;
}
示例7: 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;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
return new BitArray(bsonReader.ReadBytes());
case BsonType.Document:
bsonReader.ReadStartDocument();
var length = bsonReader.ReadInt32("Length");
var bytes = bsonReader.ReadBytes("Bytes");
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:
var message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例8: 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(char));
BsonType bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Int32:
return (char)bsonReader.ReadInt32();
case BsonType.String:
return (char)bsonReader.ReadString()[0];
default:
var message = string.Format("Cannot deserialize Char from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例9: 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(Version));
BsonType bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Document:
bsonReader.ReadStartDocument();
int major = -1, minor = -1, build = -1, revision = -1;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
switch (name)
{
case "Major": major = bsonReader.ReadInt32(); break;
case "Minor": minor = bsonReader.ReadInt32(); break;
case "Build": build = bsonReader.ReadInt32(); break;
case "Revision": revision = bsonReader.ReadInt32(); break;
default:
message = string.Format("Unrecognized element '{0}' while deserializing a Version value.", name);
throw new FileFormatException(message);
}
}
bsonReader.ReadEndDocument();
if (major == -1)
{
message = string.Format("Version missing Major element.");
throw new FileFormatException(message);
}
else if (minor == -1)
{
message = string.Format("Version missing Minor element.");
throw new FileFormatException(message);
}
else if (build == -1)
{
return new Version(major, minor);
}
else if (revision == -1)
{
return new Version(major, minor, build);
}
else
{
return new Version(major, minor, build, revision);
}
case BsonType.String:
return new Version(bsonReader.ReadString());
default:
message = string.Format("Cannot deserialize Version from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例10: 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, // ignored
IBsonSerializationOptions options)
{
VerifyDeserializeType(nominalType);
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Int32: return Enum.ToObject(nominalType, bsonReader.ReadInt32());
case BsonType.Int64: return Enum.ToObject(nominalType, bsonReader.ReadInt64());
case BsonType.Double: return Enum.ToObject(nominalType, (long)bsonReader.ReadDouble());
case BsonType.String: return Enum.Parse(nominalType, bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize {0} from BsonType {1}.", nominalType.FullName, bsonType);
throw new FileFormatException(message);
}
}