MongoDB.Bson.GuidConverter.ToBytes C# (CSharp) Метод

ToBytes() публичный статический Метод

Converts a Guid to a byte array.
public static ToBytes ( System.Guid guid, GuidRepresentation representation ) : byte[]
guid System.Guid The Guid.
representation GuidRepresentation The representation of the Guid in the byte array.
Результат byte[]
        public static byte[] ToBytes(Guid guid, GuidRepresentation representation)
        {
            var bytes = (byte[])guid.ToByteArray().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:
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes, 0, 4);
                        Array.Reverse(bytes, 4, 2);
                        Array.Reverse(bytes, 6, 2);
                    }
                    Array.Reverse(bytes, 0, 8);
                    Array.Reverse(bytes, 8, 8);
                    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 Guid to byte array because GuidRepresentation is Unspecified.");
                default:
                    throw new BsonInternalException("Unexpected GuidRepresentation.");
            }
            return bytes;
        }
    }

Usage 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
         )
 {
 }
All Usage Examples Of MongoDB.Bson.GuidConverter::ToBytes