MongoDB.Bson.GuidConverter.FromBytes C# (CSharp) Method

FromBytes() public static method

Converts a byte array to a Guid.
public static FromBytes ( byte bytes, GuidRepresentation representation ) : System.Guid
bytes byte The byte array.
representation GuidRepresentation The representation of the Guid in the byte array.
return System.Guid
        public static Guid FromBytes(byte[] bytes, GuidRepresentation representation)
        {
            if (bytes.Length != 16)
            {
                var message = string.Format("Length of byte array must be 16, not {0}.", bytes.Length);
                throw new ArgumentException(message);
            }
            bytes = (byte[])bytes.Clone();
            switch (representation)
            {
                case GuidRepresentation.CSharpLegacy:
                    if (!BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes, 0, 4);
                        Array.Reverse(bytes, 4, 2);
                        Array.Reverse(bytes, 6, 2);
                    }
                    break;
                case GuidRepresentation.JavaLegacy:
                    Array.Reverse(bytes, 0, 8);
                    Array.Reverse(bytes, 8, 8);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes, 0, 4);
                        Array.Reverse(bytes, 4, 2);
                        Array.Reverse(bytes, 6, 2);
                    }
                    break;
                case GuidRepresentation.PythonLegacy:
                case GuidRepresentation.Standard:
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes, 0, 4);
                        Array.Reverse(bytes, 4, 2);
                        Array.Reverse(bytes, 6, 2);
                    }
                    break;
                case GuidRepresentation.Unspecified:
                    throw new InvalidOperationException("Unable to convert byte array to Guid because GuidRepresentation is Unspecified.");
                default:
                    throw new BsonInternalException("Unexpected GuidRepresentation.");
            }
            return new Guid(bytes);
        }

Usage Example

Ejemplo n.º 1
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));
 }
All Usage Examples Of MongoDB.Bson.GuidConverter::FromBytes