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

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

private HandleLockedAttributes ( ConfigurationElement source ) : void
source ConfigurationElement
Результат void
        internal void HandleLockedAttributes(ConfigurationElement source) {
            // if there are locked attributes on this collection element
            if (source != null) {
                if (source._lockedAttributesList != null || source._lockedAllExceptAttributesList != null) {
                    // enumerate the possible locked properties
                    foreach (PropertyInformation propInfo in source.ElementInformation.Properties) {
                        if ((source._lockedAttributesList != null && (source._lockedAttributesList.Contains(propInfo.Name) ||
                            source._lockedAttributesList.Contains(LockAll))) ||
                            (source._lockedAllExceptAttributesList != null && !source._lockedAllExceptAttributesList.Contains(propInfo.Name))
                           ) {
                            // if the attribute has been locked in the source then check to see
                            // if the local config is trying to override it
                            if (propInfo.Name != LockAttributesKey && propInfo.Name != LockAllAttributesExceptKey) {

                                if (ElementInformation.Properties[propInfo.Name] == null) { // locked items are not defined
                                    
                                    ConfigurationPropertyCollection props = Properties; // so create the property based in the source item
                                    ConfigurationProperty prop = (ConfigurationProperty)source.Properties[propInfo.Name];
                                    props.Add(prop); // Add the property information to the property bag
                                    _evaluationElement = null; // flush the cached element data

                                    // Add the data from the source element but mark it as in herited
                                    // This must use setvalue in order to set the lock and inherited flags
                                    ConfigurationValueFlags flags = ConfigurationValueFlags.Inherited | ConfigurationValueFlags.Locked;
                                    _values.SetValue(propInfo.Name, propInfo.Value, flags, source.PropertyInfoInternal(propInfo.Name));

                                }
                                else { // don't error when optional attibute are not defined yet
                                    if (ElementInformation.Properties[propInfo.Name].ValueOrigin == PropertyValueOrigin.SetHere) {
                                        // Don't allow the override
                                        throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_attribute_locked, propInfo.Name));
                                    }
                                    // They did not override so we need to make sure the value comes from the locked one
                                    ElementInformation.Properties[propInfo.Name].Value = propInfo.Value;
                                }
                            }
                        }
                    }
                }
            }
        }

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::HandleLockedAttributes