MongoDB.Bson.IO.BsonWriter.CheckElementName C# (CSharp) Method

CheckElementName() protected method

Checks that the element name is valid.
protected CheckElementName ( string name ) : void
name string The element name to be checked.
return void
        protected void CheckElementName(string name)
        {
            if (_checkUpdateDocument)
            {
                _checkElementNames = name == "" || name[0] != '$';
                _checkUpdateDocument = false;
                return;
            }

            if (_checkElementNames)
            {
                if (name == "")
                {
                    var message = "Element name '' is not valid because it is an empty string.";
                    throw new BsonSerializationException(message);
                }

                if (name[0] == '$')
                {
                    // a few element names starting with $ have to be allowed for historical reasons
                    switch (name)
                    {
                        case "$code":
                        case "$db":
                        case "$id":
                        case "$ref":
                        case "$scope":
                            break;
                        default:
                            var message = string.Format("Element name '{0}' is not valid because it starts with a '$'.", name);
                            throw new BsonSerializationException(message);
                    }
                }

                if (name.IndexOf('.') != -1)
                {
                    var message = string.Format("Element name '{0}' is not valid because it contains a '.'.", name);
                    throw new BsonSerializationException(message);
                }
            }
        }