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

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

private MergeLocks ( ConfigurationElement source ) : void
source ConfigurationElement
Результат void
        internal void MergeLocks(ConfigurationElement source) {
            if (source != null) {
                _fItemLocked = ((source._fItemLocked & ConfigurationValueFlags.Locked) != 0) ?
                    (ConfigurationValueFlags.Inherited | source._fItemLocked) : _fItemLocked;

                if (source._lockedAttributesList != null) {
                    if (_lockedAttributesList == null) {
                        _lockedAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedAttributes);
                    }
                    foreach (string key in source._lockedAttributesList)
                        _lockedAttributesList.Add(key, ConfigurationValueFlags.Inherited);  // Mark entry as from the parent - read only
                }
                if (source._lockedAllExceptAttributesList != null) {
                    if (_lockedAllExceptAttributesList == null) {
                        _lockedAllExceptAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedExceptionList, String.Empty, source._lockedAllExceptAttributesList);
                    }

                    StringCollection intersectionCollection = IntersectLockCollections(_lockedAllExceptAttributesList, source._lockedAllExceptAttributesList);

                    _lockedAllExceptAttributesList.ClearInternal(false);
                    foreach (string key in intersectionCollection) {
                        _lockedAllExceptAttributesList.Add(key, ConfigurationValueFlags.Default);
                    }

                }
                if (source._lockedElementsList != null) {
                    if (_lockedElementsList == null) {
                        _lockedElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElements);
                    }

                    ConfigurationElementCollection collection = null;
                    if (Properties.DefaultCollectionProperty != null) // this is not a collection but it may contain a default collection
                {
                        collection = this[Properties.DefaultCollectionProperty] as ConfigurationElementCollection;
                        if (collection != null) {
                            collection.internalElementTagName = source.ElementTagName; // Default collections don't know there tag name
                            if (collection._lockedElementsList == null) {
                                collection._lockedElementsList = _lockedElementsList; //point to the same instance of the collection from parent
                            }
                        }
                    }

                    foreach (string key in source._lockedElementsList) {
                        _lockedElementsList.Add(key, ConfigurationValueFlags.Inherited);  // Mark entry as from the parent - read only
                        if (collection != null) {
                            collection._lockedElementsList.Add(key, ConfigurationValueFlags.Inherited);  // add the local copy
                        }
                    }
                }

                if (source._lockedAllExceptElementsList != null) {
                    if (_lockedAllExceptElementsList == null || _lockedAllExceptElementsList.Count == 0) {
                        _lockedAllExceptElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElementsExceptionList, source._elementTagName, source._lockedAllExceptElementsList);
                    }
                    StringCollection intersectionCollection = IntersectLockCollections(_lockedAllExceptElementsList, source._lockedAllExceptElementsList);

                    ConfigurationElementCollection collection = null;
                    if (Properties.DefaultCollectionProperty != null) { // this is not a collection but it may contain a default collection
                        collection = this[Properties.DefaultCollectionProperty] as ConfigurationElementCollection;
                        if (collection != null && collection._lockedAllExceptElementsList == null) {
                            // point default collection to the parent collection
                            collection._lockedAllExceptElementsList = _lockedAllExceptElementsList;
                        }
                    }
                    _lockedAllExceptElementsList.ClearInternal(false);
                    foreach (string key in intersectionCollection) {
                        if (!_lockedAllExceptElementsList.Contains(key) || key == ElementTagName)
                            _lockedAllExceptElementsList.Add(key, ConfigurationValueFlags.Default);  // add the local copy
                    }
                    if (_lockedAllExceptElementsList.HasParentElements) {
                        foreach (ConfigurationProperty prop in Properties) {
                            if ((!_lockedAllExceptElementsList.Contains(prop.Name)) &&
                                typeof(ConfigurationElement).IsAssignableFrom(prop.Type)) {
                                ((ConfigurationElement)this[prop]).SetLocked();
                            }
                        }
                    }
                }
            }
        }

Usage Example

 private void BaseAdd(ConfigurationElement element, bool throwIfExists, bool ignoreLocks)
 {
     bool flagAsReplaced = false;
     bool internalAddToEnd = this.internalAddToEnd;
     if (this.IsReadOnly())
     {
         throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_base_read_only"));
     }
     if (base.LockItem && !ignoreLocks)
     {
         throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_base_element_locked", new object[] { this._addElement }));
     }
     object elementKeyInternal = this.GetElementKeyInternal(element);
     int index = -1;
     for (int i = 0; i < this._items.Count; i++)
     {
         Entry entry = (Entry) this._items[i];
         if (this.CompareKeys(elementKeyInternal, entry.GetKey(this)))
         {
             if (((entry._value != null) && entry._value.LockItem) && !ignoreLocks)
             {
                 throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_base_collection_item_locked"));
             }
             if ((entry._entryType != EntryType.Removed) && throwIfExists)
             {
                 if (!element.Equals(entry._value))
                 {
                     throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_base_collection_entry_already_exists", new object[] { elementKeyInternal }), element.PropertyFileName(""), element.PropertyLineNumber(""));
                 }
                 entry._value = element;
                 return;
             }
             if (entry._entryType != EntryType.Added)
             {
                 if (((this.CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap) || (this.CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate)) && ((entry._entryType == EntryType.Removed) && (this._removedItemCount > 0)))
                 {
                     this._removedItemCount--;
                 }
                 entry._entryType = EntryType.Replaced;
                 flagAsReplaced = true;
             }
             if (!internalAddToEnd && (this.CollectionType != ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
             {
                 if (!ignoreLocks)
                 {
                     element.HandleLockedAttributes(entry._value);
                     element.MergeLocks(entry._value);
                 }
                 entry._value = element;
                 this.bModified = true;
                 return;
             }
             index = i;
             if (entry._entryType == EntryType.Added)
             {
                 internalAddToEnd = true;
             }
             break;
         }
     }
     if (index >= 0)
     {
         this._items.RemoveAt(index);
         if ((this.CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate) && (index > ((this.Count + this._removedItemCount) - this._inheritedCount)))
         {
             this._inheritedCount--;
         }
     }
     this.BaseAddInternal(internalAddToEnd ? -1 : index, element, flagAsReplaced, ignoreLocks);
     this.bModified = true;
 }
All Usage Examples Of System.Configuration.ConfigurationElement::MergeLocks