System.Configuration.ConfigurationElement.ReportInvalidLock C# (CSharp) Метод

ReportInvalidLock() приватный Метод

private ReportInvalidLock ( string attribToLockTrim, ConfigurationLockCollectionType lockedType, ConfigurationValue value, String collectionProperties ) : void
attribToLockTrim string
lockedType ConfigurationLockCollectionType
value ConfigurationValue
collectionProperties String
Результат void
        internal void ReportInvalidLock(string attribToLockTrim, ConfigurationLockCollectionType lockedType, ConfigurationValue value, String collectionProperties) {
            StringBuilder sb;
            sb = new StringBuilder();

            // Add the collection properties when locking elements
            if (!String.IsNullOrEmpty(collectionProperties) &&
                    ((lockedType == ConfigurationLockCollectionType.LockedElements) || (lockedType == ConfigurationLockCollectionType.LockedElementsExceptionList))) {
                if (sb.Length != 0)
                    sb.Append(',');
                sb.Append(collectionProperties);
            }

            // construct a list of valid lockable properties
            foreach (object _prop in Properties) {
                ConfigurationProperty validProp = (ConfigurationProperty)_prop;
                if (validProp.Name != LockAttributesKey &&
                    validProp.Name != LockAllAttributesExceptKey &&
                    validProp.Name != LockElementsKey &&
                    validProp.Name != LockAllElementsExceptKey
                ) {
                    if ((lockedType == ConfigurationLockCollectionType.LockedElements) ||
                            (lockedType == ConfigurationLockCollectionType.LockedElementsExceptionList)) {
                        if (typeof(ConfigurationElement).IsAssignableFrom(validProp.Type)) {
                            if (sb.Length != 0)
                                sb.Append(", ");
                            sb.Append("'");
                            sb.Append(validProp.Name);
                            sb.Append("'");
                        }
                    }
                    else {
                        if (!typeof(ConfigurationElement).IsAssignableFrom(validProp.Type)) {
                            if (sb.Length != 0)
                                sb.Append(", ");
                            sb.Append("'");
                            sb.Append(validProp.Name);
                            sb.Append("'");
                        }
                    }
                }
            }

            string format = null;

            if ((lockedType == ConfigurationLockCollectionType.LockedElements) ||
                    (lockedType == ConfigurationLockCollectionType.LockedElementsExceptionList)) {
                if (value != null)
                    format = SR.GetString(SR.Config_base_invalid_element_to_lock);
                else
                    format = SR.GetString(SR.Config_base_invalid_element_to_lock_by_add);

            }
            else {
                if (value != null)
                    format = SR.GetString(SR.Config_base_invalid_attribute_to_lock);
                else
                    format = SR.GetString(SR.Config_base_invalid_attribute_to_lock_by_add);
            }
            if (value != null)
                throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, format, attribToLockTrim, sb.ToString()), value.SourceInfo.FileName, value.SourceInfo.LineNumber);
            else
                throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, format, attribToLockTrim, sb.ToString()));
        }

Usage Example

Пример #1
0
        public void Add(string name)
        {
            if (((_thisElement.ItemLocked & ConfigurationValueFlags.Locked) != 0) &&
                ((_thisElement.ItemLocked & ConfigurationValueFlags.Inherited) != 0))
            {
                throw new ConfigurationErrorsException(SR.Format(SR.Config_base_attribute_locked, name));
            }

            ConfigurationValueFlags flags = ConfigurationValueFlags.Modified;

            string attribToLockTrim          = name.Trim();
            ConfigurationProperty propToLock = _thisElement.Properties[attribToLockTrim];

            if ((propToLock == null) && (attribToLockTrim != LockAll))
            {
                ConfigurationElementCollection collection = _thisElement as ConfigurationElementCollection;
                if ((collection == null) && (_thisElement.Properties.DefaultCollectionProperty != null))
                {
                    // this is not a collection but it may contain a default collection
                    collection =
                        _thisElement[_thisElement.Properties.DefaultCollectionProperty] as
                        ConfigurationElementCollection;
                }

                if ((collection == null) ||
                    (LockType == ConfigurationLockCollectionType.LockedAttributes) ||
                    // If the collection type is not element then the lock is bogus
                    (LockType == ConfigurationLockCollectionType.LockedExceptionList))
                {
                    _thisElement.ReportInvalidLock(attribToLockTrim, LockType, null, null);
                }
                else
                {
                    if (!collection.IsLockableElement(attribToLockTrim))
                    {
                        _thisElement.ReportInvalidLock(attribToLockTrim, LockType, null, collection.LockableElements);
                    }
                }
            }
            else
            {
                // the lock is in the property bag but is it the correct type?
                if ((propToLock != null) && propToLock.IsRequired)
                {
                    throw new ConfigurationErrorsException(SR.Format(SR.Config_base_required_attribute_lock_attempt,
                                                                     propToLock.Name));
                }

                if (attribToLockTrim != LockAll)
                {
                    if ((LockType == ConfigurationLockCollectionType.LockedElements) ||
                        (LockType == ConfigurationLockCollectionType.LockedElementsExceptionList))
                    {
                        // If it is an element then it must be derived from ConfigurationElement
                        if (!typeof(ConfigurationElement).IsAssignableFrom(propToLock?.Type))
                        {
                            _thisElement.ReportInvalidLock(attribToLockTrim, LockType, null, null);
                        }
                    }
                    else
                    {
                        // if it is a property then it cannot be derived from ConfigurationElement
                        if (typeof(ConfigurationElement).IsAssignableFrom(propToLock?.Type))
                        {
                            _thisElement.ReportInvalidLock(attribToLockTrim, LockType, null, null);
                        }
                    }
                }
            }

            if (_internalDictionary.Contains(name))
            {
                flags = ConfigurationValueFlags.Modified | (ConfigurationValueFlags)_internalDictionary[name];
                _internalDictionary.Remove(name); // not from parent
                _internalArraylist.Remove(name);
            }
            _internalDictionary.Add(name, flags); // not from parent
            _internalArraylist.Add(name);
            IsModified = true;
        }