/// <summary>
        /// Reads a typed array.
        /// </summary>
        /// <param name="T">Element type.</param>
        /// <param name="Provider">Database provider object.</param>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>String value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static Array ReadArray(Type T, MongoDBProvider Provider, IBsonReader Reader, BsonType FieldDataType)
        {
            switch (FieldDataType)
            {
            case BsonType.Array:
                List <object>     Elements = new List <object>();
                IObjectSerializer S        = Provider.GetObjectSerializer(T ?? typeof(GenericObject));

                Reader.ReadStartArray();
                while (Reader.State != BsonReaderState.EndOfArray)
                {
                    BsonType?ElementType = null;

                    if (Reader.State == BsonReaderState.Type)
                    {
                        ElementType = Reader.ReadBsonType();
                        if (ElementType == BsonType.EndOfDocument)
                        {
                            break;
                        }
                    }

                    Elements.Add(S.Deserialize(Reader, ElementType, true));
                }

                Reader.ReadEndArray();

                if (T is null)
                {
                    return(Elements.ToArray());
                }

                int   c      = Elements.Count;
                Array Result = Array.CreateInstance(T, c);
                Array.Copy(Elements.ToArray(), 0, Result, 0, c);

                return(Result);

            case BsonType.Binary:
                byte[] Bin = Reader.ReadBytes();

                if (T is null || T == typeof(byte))
                {
                    return(Bin);
                }

                c      = Bin.Length;
                Result = Array.CreateInstance(T, c);
                Array.Copy(Bin, 0, Result, 0, c);

                return(Result);

            case BsonType.Null:
                Reader.ReadNull();
                return(null);

            default:
                throw new Exception("Array expected.");
            }
        }
        /// <summary>
        /// Writes an array.
        /// </summary>
        /// <param name="T">Element type.</param>
        /// <param name="Provider">Database provider object.</param>
        /// <param name="Writer">Binary writer.</param>
        /// <param name="Value">Value to serialize.</param>
        public static void WriteArray(Type T, MongoDBProvider Provider, BsonWriter Writer, Array Value)
        {
            if (Value is null)
            {
                Writer.WriteNull();
            }
            else
            {
                Type LastType       = T;
                IObjectSerializer S = Provider.GetObjectSerializer(LastType);
                Type ItemType;
                bool Nullable = S.IsNullable;

                Writer.WriteStartArray();

                foreach (object Item in Value)
                {
                    if (Item == null)
                    {
                        if (Nullable)
                        {
                            Writer.WriteNull();
                        }
                        else
                        {
                            throw new Exception("Elements cannot be null.");
                        }
                    }
                    else
                    {
                        ItemType = Item.GetType();
                        if (ItemType != LastType)
                        {
                            S        = Provider.GetObjectSerializer(ItemType);
                            LastType = ItemType;
                        }

                        S.Serialize(Writer, Nullable, true, Item);
                    }
                }

                Writer.WriteEndArray();
            }
        }
        /// <summary>
        /// Reads a typed array.
        /// </summary>
        /// <typeparam name="T">Element type.</typeparam>
        /// <param name="Provider">Database provider object.</param>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>String value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static T[] ReadArray <T>(MongoDBProvider Provider, IBsonReader Reader, BsonType FieldDataType)
        {
            switch (FieldDataType)
            {
            case BsonType.Array:
                List <T>          Elements = new List <T>();
                IObjectSerializer S        = Provider.GetObjectSerializer(typeof(T));

                Reader.ReadStartArray();
                while (Reader.State != BsonReaderState.EndOfArray)
                {
                    BsonType?ElementType = null;

                    if (Reader.State == BsonReaderState.Type)
                    {
                        ElementType = Reader.ReadBsonType();
                        if (ElementType == BsonType.EndOfDocument)
                        {
                            break;
                        }
                    }

                    Elements.Add((T)S.Deserialize(Reader, ElementType, true));
                }

                Reader.ReadEndArray();

                return(Elements.ToArray());

            case BsonType.Binary:
                object Temp = Reader.ReadBytes();
                return((T[])Temp);

            case BsonType.Null:
                Reader.ReadNull();
                return(null);

            default:
                throw new Exception("Array expected.");
            }
        }