MongoDB.Bson.IO.BsonWriter.ThrowInvalidState C# (CSharp) 메소드

ThrowInvalidState() 보호된 메소드

Throws an InvalidOperationException when the method called is not valid for the current state.
protected ThrowInvalidState ( string methodName ) : void
methodName string The name of the method.
리턴 void
        protected void ThrowInvalidState(string methodName, params BsonWriterState[] validStates)
        {
            string message;
            if (_state == BsonWriterState.Initial || _state == BsonWriterState.ScopeDocument || _state == BsonWriterState.Done)
            {
                if (!methodName.StartsWith("End", StringComparison.Ordinal) && methodName != "WriteName")
                {
                    var typeName = methodName.Substring(5);
                    if (typeName.StartsWith("Start", StringComparison.Ordinal))
                    {
                        typeName = typeName.Substring(5);
                    }
                    var article = "A";
                    if (new char[] { 'A', 'E', 'I', 'O', 'U' }.Contains(typeName[0]))
                    {
                        article = "An";
                    }
                    message = string.Format(
                        "{0} {1} value cannot be written to the root level of a BSON document.",
                        article, typeName);
                    throw new InvalidOperationException(message);
                }
            }

            var validStatesString = string.Join(" or ", validStates.Select(s => s.ToString()).ToArray());
            message = string.Format(
                "{0} can only be called when State is {1}, not when State is {2}",
                methodName, validStatesString, _state);
            throw new InvalidOperationException(message);
        }
    }