コード例 #1
0
        /// <summary>
        /// Reads a BsonType from the reader.
        /// </summary>
        /// <typeparam name="TValue">The type of the BsonTrie values.</typeparam>
        /// <param name="bsonTrie">An optional trie to search for a value that matches the next element name.</param>
        /// <param name="found">Set to true if a matching value was found in the trie.</param>
        /// <param name="value">Set to the matching value found in the trie or null if no matching value was found.</param>
        /// <returns>A BsonType.</returns>
        public override BsonType ReadBsonType <TValue>(BsonTrie <TValue> bsonTrie, out bool found, out TValue value)
        {
            if (Disposed)
            {
                ThrowObjectDisposedException();
            }
            found = false;
            value = default(TValue);
            if (State == BsonReaderState.Initial || State == BsonReaderState.ScopeDocument)
            {
                // there is an implied type of Document for the top level and for scope documents
                CurrentBsonType = BsonType.Document;
                State           = BsonReaderState.Value;
                return(CurrentBsonType);
            }
            if (State != BsonReaderState.Type)
            {
                ThrowInvalidState("ReadBsonType", BsonReaderState.Type);
            }

            switch (_context.ContextType)
            {
            case ContextType.Array:
                _currentValue = _context.GetNextValue();
                if (_currentValue == null)
                {
                    State = BsonReaderState.EndOfArray;
                    return(BsonType.EndOfDocument);
                }
                State = BsonReaderState.Value;
                break;

            case ContextType.Document:
                var currentElement = _context.GetNextElement();
                if (currentElement == null)
                {
                    State = BsonReaderState.EndOfDocument;
                    return(BsonType.EndOfDocument);
                }
                if (bsonTrie != null)
                {
                    found = bsonTrie.TryGetValue(currentElement.Name, out value);
                }
                CurrentName   = currentElement.Name;
                _currentValue = currentElement.Value;
                State         = BsonReaderState.Name;
                break;

            default:
                throw new BsonInternalException("Invalid ContextType.");
            }

            CurrentBsonType = _currentValue.BsonType;
            return(CurrentBsonType);
        }