ConfigurationLockCollection
Contains a collection of locked configuration objects. This class cannot be inherited.
using System.Collections;
using System.Collections.Specialized;
using System.Text;
namespace System.Configuration
{
public sealed class ConfigurationLockCollection : ICollection, IEnumerable
{
private const string LockAll = "*";
private readonly string _ignoreName;
private readonly ConfigurationElement _thisElement;
private readonly ArrayList _internalArraylist;
private readonly HybridDictionary _internalDictionary;
private string _seedList = string.Empty;
internal ConfigurationLockCollectionType LockType { get; }
public bool IsModified { get; set; }
internal bool ExceptionList { get; }
public string AttributeList {
get {
StringBuilder stringBuilder = new StringBuilder();
IDictionaryEnumerator enumerator = _internalDictionary.GetEnumerator();
try {
while (enumerator.MoveNext()) {
DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
if (stringBuilder.Length != 0)
stringBuilder.Append(',');
stringBuilder.Append(dictionaryEntry.Key);
}
} finally {
(enumerator as IDisposable)?.Dispose();
}
return stringBuilder.ToString();
}
}
public bool HasParentElements {
get {
bool result = false;
if (!ExceptionList || _internalDictionary.Count != 0 || string.IsNullOrEmpty(_seedList)) {
IDictionaryEnumerator enumerator = _internalDictionary.GetEnumerator();
try {
while (enumerator.MoveNext()) {
if (((ConfigurationValueFlags)((DictionaryEntry)enumerator.Current).Value & ConfigurationValueFlags.Inherited) != 0)
return true;
}
return result;
} finally {
(enumerator as IDisposable)?.Dispose();
}
}
return true;
}
}
public int Count => _internalDictionary.Count;
public bool IsSynchronized => false;
public object SyncRoot => this;
internal ConfigurationLockCollection(ConfigurationElement thisElement)
: this(thisElement, ConfigurationLockCollectionType.LockedAttributes)
{
}
internal ConfigurationLockCollection(ConfigurationElement thisElement, ConfigurationLockCollectionType lockType)
: this(thisElement, lockType, string.Empty)
{
}
internal ConfigurationLockCollection(ConfigurationElement thisElement, ConfigurationLockCollectionType lockType, string ignoreName)
: this(thisElement, lockType, ignoreName, null)
{
}
internal ConfigurationLockCollection(ConfigurationElement thisElement, ConfigurationLockCollectionType lockType, string ignoreName, ConfigurationLockCollection parentCollection)
{
_thisElement = thisElement;
LockType = lockType;
_internalDictionary = new HybridDictionary();
_internalArraylist = new ArrayList();
IsModified = false;
ExceptionList = (LockType == ConfigurationLockCollectionType.LockedExceptionList || LockType == ConfigurationLockCollectionType.LockedElementsExceptionList);
_ignoreName = ignoreName;
if (parentCollection != null) {
foreach (string item in parentCollection) {
Add(item, ConfigurationValueFlags.Inherited);
if (ExceptionList) {
if (_seedList.Length != 0)
_seedList += ",";
_seedList += item;
}
}
}
}
void ICollection.CopyTo(Array array, int index)
{
_internalArraylist.CopyTo(array, index);
}
public IEnumerator GetEnumerator()
{
return _internalArraylist.GetEnumerator();
}
internal void ClearSeedList()
{
_seedList = string.Empty;
}
public void Add(string name)
{
if ((_thisElement.ItemLocked & ConfigurationValueFlags.Locked) != 0 && (_thisElement.ItemLocked & ConfigurationValueFlags.Inherited) != 0)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_attribute_locked, name));
ConfigurationValueFlags configurationValueFlags = ConfigurationValueFlags.Modified;
string text = name.Trim();
ConfigurationProperty configurationProperty = _thisElement.Properties[text];
if (configurationProperty == null && text != "*") {
ConfigurationElementCollection configurationElementCollection = _thisElement as ConfigurationElementCollection;
if (configurationElementCollection == null && _thisElement.Properties.DefaultCollectionProperty != null)
configurationElementCollection = (_thisElement[_thisElement.Properties.DefaultCollectionProperty] as ConfigurationElementCollection);
if (configurationElementCollection == null || LockType == ConfigurationLockCollectionType.LockedAttributes || LockType == ConfigurationLockCollectionType.LockedExceptionList)
_thisElement.ReportInvalidLock(text, LockType, null, null);
else if (!configurationElementCollection.IsLockableElement(text)) {
_thisElement.ReportInvalidLock(text, LockType, null, configurationElementCollection.LockableElements);
}
} else {
if (configurationProperty != null && configurationProperty.IsRequired)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_required_attribute_lock_attempt, configurationProperty.Name));
if (text != "*") {
if (LockType == ConfigurationLockCollectionType.LockedElements || LockType == ConfigurationLockCollectionType.LockedElementsExceptionList) {
if (!typeof(ConfigurationElement).IsAssignableFrom(configurationProperty?.Type))
_thisElement.ReportInvalidLock(text, LockType, null, null);
} else if (typeof(ConfigurationElement).IsAssignableFrom(configurationProperty?.Type)) {
_thisElement.ReportInvalidLock(text, LockType, null, null);
}
}
}
if (_internalDictionary.Contains(name)) {
configurationValueFlags = (ConfigurationValueFlags.Modified | (ConfigurationValueFlags)_internalDictionary[name]);
_internalDictionary.Remove(name);
_internalArraylist.Remove(name);
}
_internalDictionary.Add(name, configurationValueFlags);
_internalArraylist.Add(name);
IsModified = true;
}
internal void Add(string name, ConfigurationValueFlags flags)
{
if (flags != ConfigurationValueFlags.Inherited && _internalDictionary.Contains(name)) {
flags = (ConfigurationValueFlags.Modified | (ConfigurationValueFlags)_internalDictionary[name]);
_internalDictionary.Remove(name);
_internalArraylist.Remove(name);
}
_internalDictionary.Add(name, flags);
_internalArraylist.Add(name);
}
internal bool DefinedInParent(string name)
{
if (name == null)
return false;
if (!ExceptionList) {
if (_internalDictionary.Contains(name))
return ((ConfigurationValueFlags)_internalDictionary[name] & ConfigurationValueFlags.Inherited) != ConfigurationValueFlags.Default;
return false;
}
string text = "," + _seedList + ",";
if (name.Equals(_ignoreName) || text.Contains("," + name + ",", StringComparison.Ordinal))
return true;
if (_internalDictionary.Contains(name))
return ((ConfigurationValueFlags)_internalDictionary[name] & ConfigurationValueFlags.Inherited) != ConfigurationValueFlags.Default;
return false;
}
internal bool IsValueModified(string name)
{
if (_internalDictionary.Contains(name))
return ((ConfigurationValueFlags)_internalDictionary[name] & ConfigurationValueFlags.Modified) != ConfigurationValueFlags.Default;
return false;
}
internal void RemoveInheritedLocks()
{
StringCollection stringCollection = new StringCollection();
IEnumerator enumerator = GetEnumerator();
try {
while (enumerator.MoveNext()) {
string text = (string)enumerator.Current;
if (DefinedInParent(text))
stringCollection.Add(text);
}
} finally {
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
disposable.Dispose();
}
StringEnumerator enumerator2 = stringCollection.GetEnumerator();
try {
while (enumerator2.MoveNext()) {
string current = enumerator2.Current;
_internalDictionary.Remove(current);
_internalArraylist.Remove(current);
}
} finally {
(enumerator2 as IDisposable)?.Dispose();
}
}
public void Remove(string name)
{
if (!_internalDictionary.Contains(name))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_collection_entry_not_found, name));
if (!ExceptionList && ((ConfigurationValueFlags)_internalDictionary[name] & ConfigurationValueFlags.Inherited) != 0) {
if (((ConfigurationValueFlags)_internalDictionary[name] & ConfigurationValueFlags.Modified) == ConfigurationValueFlags.Default)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_attribute_locked, name));
ConfigurationValueFlags configurationValueFlags = (ConfigurationValueFlags)_internalDictionary[name];
configurationValueFlags &= ~ConfigurationValueFlags.Modified;
_internalDictionary[name] = configurationValueFlags;
IsModified = true;
} else {
_internalDictionary.Remove(name);
_internalArraylist.Remove(name);
IsModified = true;
}
}
internal void ClearInternal(bool useSeedIfAvailable)
{
ArrayList arrayList = new ArrayList();
IDictionaryEnumerator enumerator = _internalDictionary.GetEnumerator();
try {
while (enumerator.MoveNext()) {
DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
if (((ConfigurationValueFlags)dictionaryEntry.Value & ConfigurationValueFlags.Inherited) == ConfigurationValueFlags.Default || ExceptionList)
arrayList.Add(dictionaryEntry.Key);
}
} finally {
(enumerator as IDisposable)?.Dispose();
}
foreach (object item in arrayList) {
_internalDictionary.Remove(item);
_internalArraylist.Remove(item);
}
if (useSeedIfAvailable && !string.IsNullOrEmpty(_seedList)) {
string[] array = _seedList.Split(',', StringSplitOptions.None);
string[] array2 = array;
foreach (string name in array2) {
Add(name, ConfigurationValueFlags.Inherited);
}
}
IsModified = true;
}
public void Clear()
{
ClearInternal(true);
}
public bool Contains(string name)
{
if (ExceptionList && name.Equals(_ignoreName))
return true;
return _internalDictionary.Contains(name);
}
public void CopyTo(string[] array, int index)
{
((ICollection)this).CopyTo((Array)array, index);
}
internal void ResetModified()
{
IsModified = false;
}
public bool IsReadOnly(string name)
{
if (!_internalDictionary.Contains(name))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_collection_entry_not_found, name));
return ((ConfigurationValueFlags)_internalDictionary[name] & ConfigurationValueFlags.Inherited) != ConfigurationValueFlags.Default;
}
public void SetFromList(string attributeList)
{
string[] array = attributeList.Split(',', ';', ':');
Clear();
string[] array2 = array;
foreach (string text in array2) {
string name = text.Trim();
if (!Contains(name))
Add(name);
}
}
}
}