public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
{
var identity = (Identity)value;
var objectId = new ObjectId(identity.ToArray());
BsonType bsonType = options == null ? BsonType.ObjectId : ((RepresentationSerializationOptions)options).Representation;
switch (bsonType)
{
case BsonType.String:
bsonWriter.WriteString(objectId.ToString());
break;
case BsonType.ObjectId:
bsonWriter.WriteObjectId(objectId.Timestamp, objectId.Machine, objectId.Pid, objectId.Increment);
break;
default:
throw new BsonSerializationException(string.Format("'{0}' is not a valid representation for type 'Identity'", bsonType));
}
}