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);
}