A static class containing methods to convert to and from Guids and byte arrays in various byte orders.
Example #1
0
 /// <summary>
 /// Initializes a new instance of the BsonBinaryData class.
 /// </summary>
 /// <param name="guid">A Guid.</param>
 /// <param name="guidRepresentation">The representation for Guids.</param>
 public BsonBinaryData(
     Guid guid,
     GuidRepresentation guidRepresentation
     )
     : this(
         GuidConverter.ToBytes(guid, guidRepresentation),
         (guidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy,
         guidRepresentation
         )
 {
 }
Example #2
0
#pragma warning restore 618

        /// <summary>
        /// Initializes a new instance of the BsonBinaryData class.
        /// </summary>
        /// <param name="guid">A Guid.</param>
        /// <param name="guidRepresentation">The representation for Guids.</param>
        public BsonBinaryData(Guid guid, GuidRepresentation guidRepresentation)
        {
            _bytes   = GuidConverter.ToBytes(guid, guidRepresentation);
            _subType = GuidConverter.GetSubType(guidRepresentation);
#pragma warning disable 618
            if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
            {
                _guidRepresentation = guidRepresentation;
            }
#pragma warning restore 618
        }
Example #3
0
 /// <summary>
 /// Converts this BsonBinaryData to a Guid.
 /// </summary>
 /// <param name="guidRepresentation">The representation for Guids.</param>
 /// <returns>A Guid.</returns>
 public Guid ToGuid(GuidRepresentation guidRepresentation)
 {
     if (_subType != BsonBinarySubType.UuidStandard && _subType != BsonBinarySubType.UuidLegacy)
     {
         var message = string.Format("SubType must be UuidStandard or UuidLegacy, not {0}.", _subType);
         throw new InvalidOperationException(message);
     }
     if (guidRepresentation == GuidRepresentation.Unspecified)
     {
         throw new ArgumentException("GuidRepresentation cannot be Unspecified.");
     }
     return(GuidConverter.FromBytes(_bytes, guidRepresentation));
 }
Example #4
0
        /// <summary>
        /// Converts this BsonBinaryData to a Guid.
        /// </summary>
        /// <returns>A Guid.</returns>
        public Guid ToGuid()
        {
#pragma warning disable 618
            if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
            {
                return(ToGuid(_guidRepresentation));
            }
            else
            {
                if (_subType != BsonBinarySubType.UuidStandard)
                {
                    throw new InvalidOperationException("ToGuid without a Guid representation can only be called when sub type is UuidStandard.");
                }
                return(GuidConverter.FromBytes(_bytes, GuidRepresentation.Standard));
            }
#pragma warning restore 618
        }
Example #5
0
        /// <summary>
        /// Converts this BsonBinaryData to a Guid.
        /// </summary>
        /// <param name="guidRepresentation">The representation for Guids.</param>
        /// <returns>A Guid.</returns>
        public Guid ToGuid(GuidRepresentation guidRepresentation)
        {
            if (_subType != BsonBinarySubType.UuidStandard && _subType != BsonBinarySubType.UuidLegacy)
            {
                var message = string.Format("SubType must be UuidStandard or UuidLegacy, not {0}.", _subType);
                throw new InvalidOperationException(message);
            }
            if (guidRepresentation == GuidRepresentation.Unspecified)
            {
                throw new ArgumentException("GuidRepresentation cannot be Unspecified.");
            }
#pragma warning disable 618
            if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V3)
            {
                var expectedSubType = GuidConverter.GetSubType(guidRepresentation);
                if (_subType != expectedSubType)
                {
                    throw new InvalidOperationException($"ToGuid with Guid representation {guidRepresentation} can only be called when sub type is {expectedSubType}.");
                }
            }
#pragma warning restore 618

            return(GuidConverter.FromBytes(_bytes, guidRepresentation));
        }