Exemplo n.º 1
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
        }
Exemplo n.º 2
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));
        }