本文整理汇总了C#中ObjectId.ToByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectId.ToByteArray方法的具体用法?C# ObjectId.ToByteArray怎么用?C# ObjectId.ToByteArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectId
的用法示例。
在下文中一共展示了ObjectId.ToByteArray方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestByteArrayConstructor
public void TestByteArrayConstructor()
{
byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
var objectId = new ObjectId(bytes);
Assert.AreEqual(0x01020304, objectId.Timestamp);
Assert.AreEqual(0x050607, objectId.Machine);
Assert.AreEqual(0x0809, objectId.Pid);
Assert.AreEqual(0x0a0b0c, objectId.Increment);
Assert.AreEqual(0x05060708090a0b0c, objectId.MachinePidIncrement);
Assert.AreEqual(Bson.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime);
Assert.AreEqual("0102030405060708090a0b0c", objectId.ToString());
Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray()));
}
示例2: Deserialize
public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{
switch (bsonReader.CurrentBsonType)
{
case BsonType.ObjectId:
int timestamp;
int machine;
short pid;
int increment;
bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment);
var id = new ObjectId(timestamp, machine, pid, increment);
return new Identity(id.ToByteArray());
case BsonType.String:
return new Identity(bsonReader.ReadString());
default:
throw new FormatException(string.Format("Cannot deserialize Identity from BsonType: {0}", bsonReader.CurrentBsonType));
}
}
示例3: WriteObjectId
/// <inheritdoc/>
public override void WriteObjectId(ObjectId value)
{
ThrowIfDisposed();
PrepareToWrite(12);
var segment = _buffer.AccessBackingBytes(_position);
if (segment.Count >= 12)
{
value.ToByteArray(segment.Array, segment.Offset);
}
else
{
var bytes = value.ToByteArray();
_buffer.SetBytes(_position, bytes, 0, 12);
}
SetPositionAfterWrite(_position + 12);
}
示例4: WriteObjectId
/// <inheritdoc/>
public override void WriteObjectId(ObjectId value)
{
ThrowIfDisposed();
value.ToByteArray(_temp, 0);
_stream.Write(_temp, 0, 12);
}
示例5: WriteObjectId
/// <summary>
/// Writes a BSON ObjectId to the stream.
/// </summary>
/// <param name="value">The value.</param>
/// <exception cref="System.ArgumentNullException">stream</exception>
public void WriteObjectId(ObjectId value)
{
if (_bsonStream != null)
{
_bsonStream.WriteBsonObjectId(value);
}
else
{
var bytes = value.ToByteArray();
_stream.Write(bytes, 0, 12);
}
}
示例6: PrepareToWrite
/// <summary>
/// Writes a BSON ObjectId to the stream.
/// </summary>
/// <param name="value">The value.</param>
void IBsonStream.WriteBsonObjectId(ObjectId value)
{
PrepareToWrite(12);
var segment = _byteBuffer.AccessBackingBytes(_position);
if (segment.Count >= 12)
{
value.GetBytes(segment.Array, segment.Offset);
}
else
{
var bytes = value.ToByteArray();
_byteBuffer.WriteBytes(_position, bytes, 0, 12);
}
SetPositionAfterWrite(_position + 12);
}
示例7: WriteObjectId
/// <summary>
/// Writes a BSON ObjectId to the writer.
/// </summary>
/// <param name="objectId">The ObjectId.</param>
public override void WriteObjectId(ObjectId objectId)
{
if (Disposed) { throw new ObjectDisposedException("JsonWriter"); }
if (State != BsonWriterState.Value && State != BsonWriterState.Initial)
{
ThrowInvalidState("WriteObjectId", BsonWriterState.Value, BsonWriterState.Initial);
}
var bytes = objectId.ToByteArray();
WriteNameHelper(Name);
switch (_jsonWriterSettings.OutputMode)
{
case JsonOutputMode.Strict:
_textWriter.Write("{{ \"$oid\" : \"{0}\" }}", BsonUtils.ToHexString(bytes));
break;
case JsonOutputMode.Shell:
default:
_textWriter.Write("ObjectId(\"{0}\")", BsonUtils.ToHexString(bytes));
break;
}
State = GetNextState();
}
示例8: Write
public void Write(ObjectId value)
{
this.Write(value.ToByteArray());
}
示例9: Write
public static void Write(this BinaryWriter writer, ObjectId oid)
{
writer.Write(oid.ToByteArray());
}