ConfigurationElement
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Runtime.Versioning;
using System.Text;
using System.Xml;
namespace System.Configuration
{
public abstract class ConfigurationElement
{
private const string LockAttributesKey = "lockAttributes";
private const string LockAllAttributesExceptKey = "lockAllAttributesExcept";
private const string LockElementsKey = "lockElements";
private const string LockAll = "*";
private const string LockAllElementsExceptKey = "lockAllElementsExcept";
private const string LockItemKey = "lockItem";
internal const string DefaultCollectionPropertyName = "";
private static readonly string[] s_lockAttributeNames = new string[5] {
"lockAttributes",
"lockAllAttributesExcept",
"lockElements",
"lockAllElementsExcept",
"lockItem"
};
private static readonly Hashtable s_propertyBags = new Hashtable();
private static volatile Dictionary<Type, ConfigurationValidatorBase> s_perTypeValidators;
internal static readonly object s_nullPropertyValue = new object();
private static readonly ConfigurationElementProperty s_elementProperty = new ConfigurationElementProperty(new DefaultValidator());
private bool _initialized;
private bool _modified;
private bool _readOnly;
internal BaseConfigurationRecord _configRecord;
private ConfigurationElementProperty _elementProperty = s_elementProperty;
internal ContextInformation _evalContext;
private volatile ElementInformation _evaluationElement;
internal ConfigurationValueFlags _itemLockedFlag;
internal ConfigurationLockCollection _lockedAllExceptAttributesList;
internal ConfigurationLockCollection _lockedAllExceptElementsList;
internal ConfigurationLockCollection _lockedAttributesList;
internal ConfigurationLockCollection _lockedElementsList;
internal bool DataToWriteInternal { get; set; }
internal bool ElementPresent { get; set; }
internal string ElementTagName { get; set; }
internal ConfigurationLockCollection LockedAttributesList => _lockedAttributesList;
internal ConfigurationLockCollection LockedAllExceptAttributesList => _lockedAllExceptAttributesList;
internal ConfigurationValueFlags ItemLocked => _itemLockedFlag;
public ConfigurationLockCollection LockAttributes => _lockedAttributesList ?? (_lockedAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedAttributes));
public ConfigurationLockCollection LockAllAttributesExcept => _lockedAllExceptAttributesList ?? (_lockedAllExceptAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedExceptionList, ElementTagName));
public ConfigurationLockCollection LockElements => _lockedElementsList ?? (_lockedElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElements));
public ConfigurationLockCollection LockAllElementsExcept => _lockedAllExceptElementsList ?? (_lockedAllExceptElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElementsExceptionList, ElementTagName));
public bool LockItem {
get {
return (_itemLockedFlag & ConfigurationValueFlags.Locked) != ConfigurationValueFlags.Default;
}
set {
if ((_itemLockedFlag & ConfigurationValueFlags.Inherited) == ConfigurationValueFlags.Default) {
_itemLockedFlag = (value ? ConfigurationValueFlags.Locked : ConfigurationValueFlags.Default);
_itemLockedFlag |= ConfigurationValueFlags.Modified;
return;
}
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_attribute_locked, "lockItem"));
}
}
protected internal object this[ConfigurationProperty prop] {
get {
object obj = Values[prop.Name];
if (obj == null) {
if (prop.IsConfigurationElementType) {
lock (Values.SyncRoot) {
obj = Values[prop.Name];
if (obj == null) {
ConfigurationElement configurationElement = CreateElement(prop.Type);
if (_readOnly)
configurationElement.SetReadOnly();
if (typeof(ConfigurationElementCollection).IsAssignableFrom(prop.Type)) {
ConfigurationElementCollection configurationElementCollection = configurationElement as ConfigurationElementCollection;
if (prop.AddElementName != null)
configurationElementCollection.AddElementName = prop.AddElementName;
if (prop.RemoveElementName != null)
configurationElementCollection.RemoveElementName = prop.RemoveElementName;
if (prop.ClearElementName != null)
configurationElementCollection.ClearElementName = prop.ClearElementName;
}
Values.SetValue(prop.Name, configurationElement, ConfigurationValueFlags.Inherited, null);
obj = configurationElement;
}
}
} else
obj = prop.DefaultValue;
} else if (obj == s_nullPropertyValue) {
obj = null;
}
if (obj is InvalidPropValue)
throw ((InvalidPropValue)obj).Error;
return obj;
}
set {
SetPropertyValue(prop, value, false);
}
}
protected internal object this[string propertyName] {
get {
ConfigurationProperty configurationProperty = Properties[propertyName];
if (configurationProperty == null) {
configurationProperty = Properties[""];
if (configurationProperty.ProvidedName != propertyName)
return null;
}
return this[configurationProperty];
}
set {
SetPropertyValue(Properties[propertyName], value, false);
}
}
protected internal virtual ConfigurationPropertyCollection Properties {
get {
if (PropertiesFromType(GetType(), out ConfigurationPropertyCollection result))
ApplyValidatorsRecursive(this);
return result;
}
}
internal ConfigurationValues Values { get; }
public ElementInformation ElementInformation => _evaluationElement ?? (_evaluationElement = new ElementInformation(this));
protected ContextInformation EvaluationContext {
get {
if (_evalContext == null) {
if (_configRecord == null)
throw new ConfigurationErrorsException(System.SR.Config_element_no_context);
_evalContext = new ContextInformation(_configRecord);
}
return _evalContext;
}
}
protected internal virtual ConfigurationElementProperty ElementProperty => _elementProperty;
protected bool HasContext => _configRecord != null;
public Configuration CurrentConfiguration => _configRecord?.CurrentConfiguration;
protected ConfigurationElement()
{
Values = new ConfigurationValues();
ApplyValidator(this);
}
internal static bool IsNullOrNullProperty(object value)
{
if (value != null)
return value == s_nullPropertyValue;
return true;
}
internal ConfigurationElement CreateElement(Type type)
{
ConfigurationElement configurationElement = (ConfigurationElement)TypeUtil.CreateInstance(type);
configurationElement.CallInit();
return configurationElement;
}
protected internal virtual void Init()
{
_initialized = true;
}
internal void CallInit()
{
if (!_initialized) {
Init();
_initialized = true;
}
}
internal void MergeLocks(ConfigurationElement source)
{
if (source != null) {
_itemLockedFlag = (((source._itemLockedFlag & ConfigurationValueFlags.Locked) != 0) ? (ConfigurationValueFlags.Inherited | source._itemLockedFlag) : _itemLockedFlag);
if (source._lockedAttributesList != null) {
if (_lockedAttributesList == null)
_lockedAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedAttributes);
foreach (string lockedAttributes in source._lockedAttributesList) {
_lockedAttributesList.Add(lockedAttributes, ConfigurationValueFlags.Inherited);
}
}
if (source._lockedAllExceptAttributesList != null) {
if (_lockedAllExceptAttributesList == null)
_lockedAllExceptAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedExceptionList, string.Empty, source._lockedAllExceptAttributesList);
StringCollection stringCollection = IntersectLockCollections(_lockedAllExceptAttributesList, source._lockedAllExceptAttributesList);
_lockedAllExceptAttributesList.ClearInternal(false);
StringEnumerator enumerator2 = stringCollection.GetEnumerator();
try {
while (enumerator2.MoveNext()) {
string current = enumerator2.Current;
_lockedAllExceptAttributesList.Add(current, ConfigurationValueFlags.Default);
}
} finally {
(enumerator2 as IDisposable)?.Dispose();
}
}
if (source._lockedElementsList != null) {
if (_lockedElementsList == null)
_lockedElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElements);
ConfigurationElementCollection configurationElementCollection = null;
if (Properties.DefaultCollectionProperty != null) {
configurationElementCollection = (this[Properties.DefaultCollectionProperty] as ConfigurationElementCollection);
if (configurationElementCollection != null) {
configurationElementCollection.InternalElementTagName = source.ElementTagName;
if (configurationElementCollection._lockedElementsList == null)
configurationElementCollection._lockedElementsList = _lockedElementsList;
}
}
foreach (string lockedElements in source._lockedElementsList) {
_lockedElementsList.Add(lockedElements, ConfigurationValueFlags.Inherited);
configurationElementCollection?._lockedElementsList.Add(lockedElements, ConfigurationValueFlags.Inherited);
}
}
if (source._lockedAllExceptElementsList != null) {
if (_lockedAllExceptElementsList == null || _lockedAllExceptElementsList.Count == 0)
_lockedAllExceptElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElementsExceptionList, source.ElementTagName, source._lockedAllExceptElementsList);
StringCollection stringCollection2 = IntersectLockCollections(_lockedAllExceptElementsList, source._lockedAllExceptElementsList);
if (Properties.DefaultCollectionProperty != null) {
ConfigurationElementCollection configurationElementCollection2 = this[Properties.DefaultCollectionProperty] as ConfigurationElementCollection;
if (configurationElementCollection2 != null && configurationElementCollection2._lockedAllExceptElementsList == null)
configurationElementCollection2._lockedAllExceptElementsList = _lockedAllExceptElementsList;
}
_lockedAllExceptElementsList.ClearInternal(false);
StringEnumerator enumerator4 = stringCollection2.GetEnumerator();
try {
while (enumerator4.MoveNext()) {
string current2 = enumerator4.Current;
if (!_lockedAllExceptElementsList.Contains(current2) || current2 == ElementTagName)
_lockedAllExceptElementsList.Add(current2, ConfigurationValueFlags.Default);
}
} finally {
(enumerator4 as IDisposable)?.Dispose();
}
if (_lockedAllExceptElementsList.HasParentElements) {
foreach (ConfigurationProperty property in Properties) {
if (!_lockedAllExceptElementsList.Contains(property.Name) && property.IsConfigurationElementType)
((ConfigurationElement)this[property]).SetLocked();
}
}
}
}
}
internal void HandleLockedAttributes(ConfigurationElement source)
{
if (source != null && (source._lockedAttributesList != null || source._lockedAllExceptAttributesList != null)) {
foreach (PropertyInformation property2 in source.ElementInformation.Properties) {
if (((source._lockedAttributesList != null && (source._lockedAttributesList.Contains(property2.Name) || source._lockedAttributesList.Contains("*"))) || (source._lockedAllExceptAttributesList != null && !source._lockedAllExceptAttributesList.Contains(property2.Name))) && !(property2.Name == "lockAttributes") && !(property2.Name == "lockAllAttributesExcept")) {
if (ElementInformation.Properties[property2.Name] == null) {
ConfigurationPropertyCollection properties = Properties;
ConfigurationProperty property = source.Properties[property2.Name];
properties.Add(property);
_evaluationElement = null;
ConfigurationValueFlags valueFlags = ConfigurationValueFlags.Inherited | ConfigurationValueFlags.Locked;
Values.SetValue(property2.Name, property2.Value, valueFlags, source.PropertyInfoInternal(property2.Name));
} else {
if (ElementInformation.Properties[property2.Name].ValueOrigin == PropertyValueOrigin.SetHere)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_attribute_locked, property2.Name));
ElementInformation.Properties[property2.Name].Value = property2.Value;
}
}
}
}
}
internal virtual void AssociateContext(BaseConfigurationRecord configRecord)
{
_configRecord = configRecord;
Values.AssociateContext(configRecord);
}
protected internal virtual bool IsModified()
{
if (_modified || (_lockedAttributesList != null && _lockedAttributesList.IsModified) || (_lockedAllExceptAttributesList != null && _lockedAllExceptAttributesList.IsModified) || (_lockedElementsList != null && _lockedElementsList.IsModified) || (_lockedAllExceptElementsList != null && _lockedAllExceptElementsList.IsModified) || (_itemLockedFlag & ConfigurationValueFlags.Modified) != 0)
return true;
foreach (ConfigurationElement configurationElement in Values.ConfigurationElements) {
if (configurationElement.IsModified())
return true;
}
return false;
}
protected internal virtual void ResetModified()
{
_modified = false;
_lockedAttributesList?.ResetModified();
_lockedAllExceptAttributesList?.ResetModified();
_lockedElementsList?.ResetModified();
_lockedAllExceptElementsList?.ResetModified();
foreach (ConfigurationElement configurationElement in Values.ConfigurationElements) {
configurationElement.ResetModified();
}
}
public virtual bool IsReadOnly()
{
return _readOnly;
}
protected internal virtual void SetReadOnly()
{
_readOnly = true;
foreach (ConfigurationElement configurationElement in Values.ConfigurationElements) {
configurationElement.SetReadOnly();
}
}
internal void SetLocked()
{
_itemLockedFlag = (ConfigurationValueFlags.Locked | ConfigurationValueFlags.XmlParentInherited);
foreach (ConfigurationProperty property in Properties) {
ConfigurationElement configurationElement = this[property] as ConfigurationElement;
if (configurationElement != null) {
if (configurationElement.GetType() != GetType())
configurationElement.SetLocked();
ConfigurationElementCollection configurationElementCollection = this[property] as ConfigurationElementCollection;
if (configurationElementCollection != null) {
foreach (object item in configurationElementCollection) {
(item as ConfigurationElement)?.SetLocked();
}
}
}
}
}
internal ArrayList GetErrorsList()
{
ArrayList arrayList = new ArrayList();
ListErrors(arrayList);
return arrayList;
}
internal ConfigurationErrorsException GetErrors()
{
ArrayList errorsList = GetErrorsList();
if (errorsList.Count == 0)
return null;
return new ConfigurationErrorsException(errorsList);
}
protected virtual void ListErrors(IList errorList)
{
foreach (InvalidPropValue invalidValue in Values.InvalidValues) {
errorList.Add(invalidValue.Error);
}
foreach (ConfigurationElement configurationElement3 in Values.ConfigurationElements) {
configurationElement3.ListErrors(errorList);
ConfigurationElementCollection configurationElementCollection = configurationElement3 as ConfigurationElementCollection;
if (configurationElementCollection != null) {
foreach (ConfigurationElement item in configurationElementCollection) {
item.ListErrors(errorList);
}
}
}
}
protected internal virtual void InitializeDefault()
{
}
internal void CheckLockedElement(string elementName, XmlReader reader)
{
if (elementName != null && ((_lockedElementsList != null && (_lockedElementsList.DefinedInParent("*") || _lockedElementsList.DefinedInParent(elementName))) || (_lockedAllExceptElementsList != null && _lockedAllExceptElementsList.Count != 0 && _lockedAllExceptElementsList.HasParentElements && !_lockedAllExceptElementsList.DefinedInParent(elementName)) || (_itemLockedFlag & ConfigurationValueFlags.Inherited) != 0))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_element_locked, elementName), reader);
}
internal void RemoveAllInheritedLocks()
{
_lockedAttributesList?.RemoveInheritedLocks();
_lockedElementsList?.RemoveInheritedLocks();
_lockedAllExceptAttributesList?.RemoveInheritedLocks();
_lockedAllExceptElementsList?.RemoveInheritedLocks();
}
internal void ResetLockLists(ConfigurationElement parentElement)
{
_lockedAttributesList = null;
_lockedAllExceptAttributesList = null;
_lockedElementsList = null;
_lockedAllExceptElementsList = null;
if (parentElement != null) {
_itemLockedFlag = (((parentElement._itemLockedFlag & ConfigurationValueFlags.Locked) != 0) ? (ConfigurationValueFlags.Inherited | parentElement._itemLockedFlag) : ConfigurationValueFlags.Default);
if (parentElement._lockedAttributesList != null) {
_lockedAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedAttributes);
foreach (string lockedAttributes in parentElement._lockedAttributesList) {
_lockedAttributesList.Add(lockedAttributes, ConfigurationValueFlags.Inherited);
}
}
if (parentElement._lockedAllExceptAttributesList != null)
_lockedAllExceptAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedExceptionList, string.Empty, parentElement._lockedAllExceptAttributesList);
if (parentElement._lockedElementsList != null) {
_lockedElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElements);
if (Properties.DefaultCollectionProperty != null) {
ConfigurationElementCollection configurationElementCollection = this[Properties.DefaultCollectionProperty] as ConfigurationElementCollection;
if (configurationElementCollection != null) {
configurationElementCollection.InternalElementTagName = parentElement.ElementTagName;
if (configurationElementCollection._lockedElementsList == null)
configurationElementCollection._lockedElementsList = _lockedElementsList;
}
}
foreach (string lockedElements in parentElement._lockedElementsList) {
_lockedElementsList.Add(lockedElements, ConfigurationValueFlags.Inherited);
}
}
if (parentElement._lockedAllExceptElementsList != null) {
_lockedAllExceptElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElementsExceptionList, parentElement.ElementTagName, parentElement._lockedAllExceptElementsList);
if (Properties.DefaultCollectionProperty != null) {
ConfigurationElementCollection configurationElementCollection2 = this[Properties.DefaultCollectionProperty] as ConfigurationElementCollection;
if (configurationElementCollection2 != null && configurationElementCollection2._lockedAllExceptElementsList == null)
configurationElementCollection2._lockedAllExceptElementsList = _lockedAllExceptElementsList;
}
}
}
}
protected internal virtual void Reset(ConfigurationElement parentElement)
{
Values.Clear();
ResetLockLists(parentElement);
ConfigurationPropertyCollection properties = Properties;
ElementPresent = false;
if (parentElement == null)
InitializeDefault();
else {
bool flag = false;
ConfigurationPropertyCollection configurationPropertyCollection = null;
for (int i = 0; i < parentElement.Values.Count; i++) {
string key = parentElement.Values.GetKey(i);
ConfigurationValue configValue = parentElement.Values.GetConfigValue(i);
object obj = configValue?.Value;
PropertySourceInfo sourceInfo = configValue?.SourceInfo;
ConfigurationProperty configurationProperty = parentElement.Properties[key];
if (configurationProperty != null && (configurationPropertyCollection == null || configurationPropertyCollection.Contains(configurationProperty.Name))) {
if (configurationProperty.IsConfigurationElementType)
flag = true;
else {
ConfigurationValueFlags valueFlags = (ConfigurationValueFlags)(1 | (((_lockedAttributesList != null && (_lockedAttributesList.Contains(key) || _lockedAttributesList.Contains("*"))) || (_lockedAllExceptAttributesList != null && !_lockedAllExceptAttributesList.Contains(key))) ? 4 : 0));
if (obj != s_nullPropertyValue)
Values.SetValue(key, obj, valueFlags, sourceInfo);
if (!properties.Contains(key)) {
properties.Add(configurationProperty);
Values.SetValue(key, obj, valueFlags, sourceInfo);
}
}
}
}
if (flag) {
for (int j = 0; j < parentElement.Values.Count; j++) {
string key2 = parentElement.Values.GetKey(j);
object obj2 = parentElement.Values[j];
ConfigurationProperty configurationProperty2 = parentElement.Properties[key2];
if (configurationProperty2 != null && configurationProperty2.IsConfigurationElementType) {
ConfigurationElement configurationElement = (ConfigurationElement)this[configurationProperty2];
configurationElement.Reset((ConfigurationElement)obj2);
}
}
}
}
}
public override bool Equals(object compareTo)
{
ConfigurationElement configurationElement = compareTo as ConfigurationElement;
if (configurationElement == null || compareTo.GetType() != GetType() || configurationElement.Properties.Count != Properties.Count)
return false;
foreach (ConfigurationProperty property in Properties) {
object obj = Values[property.Name];
object obj2 = configurationElement.Values[property.Name];
if (!object.Equals(obj, obj2) && (!IsNullOrNullProperty(obj) || !object.Equals(obj2, property.DefaultValue)) && (!IsNullOrNullProperty(obj2) || !object.Equals(obj, property.DefaultValue)))
return false;
}
return true;
}
public override int GetHashCode()
{
int num = 0;
foreach (ConfigurationProperty property in Properties) {
object obj = this[property];
if (obj != null)
num ^= this[property].GetHashCode();
}
return num;
}
private static bool PropertiesFromType(Type type, out ConfigurationPropertyCollection result)
{
ConfigurationPropertyCollection configurationPropertyCollection = (ConfigurationPropertyCollection)s_propertyBags[type];
bool result2 = false;
if (configurationPropertyCollection == null) {
lock (s_propertyBags.SyncRoot) {
configurationPropertyCollection = (ConfigurationPropertyCollection)s_propertyBags[type];
if (configurationPropertyCollection == null) {
configurationPropertyCollection = CreatePropertyBagFromType(type);
s_propertyBags[type] = configurationPropertyCollection;
result2 = true;
}
}
}
result = configurationPropertyCollection;
return result2;
}
private static ConfigurationPropertyCollection CreatePropertyBagFromType(Type type)
{
if (typeof(ConfigurationElement).IsAssignableFrom(type)) {
ConfigurationValidatorAttribute configurationValidatorAttribute = Attribute.GetCustomAttribute(type, typeof(ConfigurationValidatorAttribute)) as ConfigurationValidatorAttribute;
if (configurationValidatorAttribute != null) {
configurationValidatorAttribute.SetDeclaringType(type);
ConfigurationValidatorBase validatorInstance = configurationValidatorAttribute.ValidatorInstance;
if (validatorInstance != null)
CachePerTypeValidator(type, validatorInstance);
}
}
ConfigurationPropertyCollection configurationPropertyCollection = new ConfigurationPropertyCollection();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (PropertyInfo propertyInformation in properties) {
ConfigurationProperty configurationProperty = CreateConfigurationPropertyFromAttributes(propertyInformation);
if (configurationProperty != null)
configurationPropertyCollection.Add(configurationProperty);
}
return configurationPropertyCollection;
}
private static ConfigurationProperty CreateConfigurationPropertyFromAttributes(PropertyInfo propertyInformation)
{
ConfigurationProperty configurationProperty = null;
ConfigurationPropertyAttribute configurationPropertyAttribute = Attribute.GetCustomAttribute(propertyInformation, typeof(ConfigurationPropertyAttribute)) as ConfigurationPropertyAttribute;
if (configurationPropertyAttribute != null)
configurationProperty = new ConfigurationProperty(propertyInformation);
if (configurationProperty != null && typeof(ConfigurationElement).IsAssignableFrom(configurationProperty.Type))
PropertiesFromType(configurationProperty.Type, out ConfigurationPropertyCollection _);
return configurationProperty;
}
private static void CachePerTypeValidator(Type type, ConfigurationValidatorBase validator)
{
if (s_perTypeValidators == null)
s_perTypeValidators = new Dictionary<Type, ConfigurationValidatorBase>();
if (!validator.CanValidate(type))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Validator_does_not_support_elem_type, type.Name));
s_perTypeValidators.Add(type, validator);
}
private static void ApplyValidatorsRecursive(ConfigurationElement root)
{
ApplyValidator(root);
foreach (ConfigurationElement configurationElement in root.Values.ConfigurationElements) {
ApplyValidatorsRecursive(configurationElement);
}
}
private static void ApplyValidator(ConfigurationElement elem)
{
if (s_perTypeValidators != null && s_perTypeValidators.ContainsKey(elem.GetType()))
elem._elementProperty = new ConfigurationElementProperty(s_perTypeValidators[elem.GetType()]);
}
protected void SetPropertyValue(ConfigurationProperty prop, object value, bool ignoreLocks)
{
if (IsReadOnly())
throw new ConfigurationErrorsException(System.SR.Config_base_read_only);
if (!ignoreLocks && ((_lockedAllExceptAttributesList != null && _lockedAllExceptAttributesList.HasParentElements && !_lockedAllExceptAttributesList.DefinedInParent(prop.Name)) || (_lockedAttributesList != null && (_lockedAttributesList.DefinedInParent(prop.Name) || _lockedAttributesList.DefinedInParent("*"))) || ((_itemLockedFlag & ConfigurationValueFlags.Locked) != 0 && (_itemLockedFlag & ConfigurationValueFlags.Inherited) != 0)))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_attribute_locked, prop.Name));
_modified = true;
if (value != null)
prop.Validate(value);
Values[prop.Name] = (value ?? s_nullPropertyValue);
}
internal PropertySourceInfo PropertyInfoInternal(string propertyName)
{
return Values.GetSourceInfo(propertyName);
}
internal string PropertyFileName(string propertyName)
{
PropertySourceInfo propertySourceInfo = PropertyInfoInternal(propertyName) ?? PropertyInfoInternal(string.Empty);
if (propertySourceInfo != null)
return propertySourceInfo.FileName;
return string.Empty;
}
internal int PropertyLineNumber(string propertyName)
{
return (PropertyInfoInternal(propertyName) ?? PropertyInfoInternal(string.Empty))?.LineNumber ?? 0;
}
internal virtual void Dump(TextWriter tw)
{
tw.WriteLine("Type: " + GetType().FullName);
PropertyInfo[] properties = GetType().GetProperties();
foreach (PropertyInfo propertyInfo in properties) {
tw.WriteLine("{0}: {1}", propertyInfo.Name, propertyInfo.GetValue(this, null));
}
}
protected internal virtual void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
{
if (sourceElement != null) {
bool flag = false;
_lockedAllExceptAttributesList = sourceElement._lockedAllExceptAttributesList;
_lockedAllExceptElementsList = sourceElement._lockedAllExceptElementsList;
_itemLockedFlag = sourceElement._itemLockedFlag;
_lockedAttributesList = sourceElement._lockedAttributesList;
_lockedElementsList = sourceElement._lockedElementsList;
AssociateContext(sourceElement._configRecord);
if (parentElement != null) {
if (parentElement._lockedAttributesList != null)
_lockedAttributesList = UnMergeLockList(sourceElement._lockedAttributesList, parentElement._lockedAttributesList, saveMode);
if (parentElement._lockedElementsList != null)
_lockedElementsList = UnMergeLockList(sourceElement._lockedElementsList, parentElement._lockedElementsList, saveMode);
if (parentElement._lockedAllExceptAttributesList != null)
_lockedAllExceptAttributesList = UnMergeLockList(sourceElement._lockedAllExceptAttributesList, parentElement._lockedAllExceptAttributesList, saveMode);
if (parentElement._lockedAllExceptElementsList != null)
_lockedAllExceptElementsList = UnMergeLockList(sourceElement._lockedAllExceptElementsList, parentElement._lockedAllExceptElementsList, saveMode);
}
ConfigurationPropertyCollection properties = Properties;
ConfigurationPropertyCollection configurationPropertyCollection = null;
for (int i = 0; i < sourceElement.Values.Count; i++) {
string key = sourceElement.Values.GetKey(i);
object obj = sourceElement.Values[i];
ConfigurationProperty configurationProperty = sourceElement.Properties[key];
if (configurationProperty != null && (configurationPropertyCollection == null || configurationPropertyCollection.Contains(configurationProperty.Name))) {
if (configurationProperty.IsConfigurationElementType)
flag = true;
else if (obj != s_nullPropertyValue && !properties.Contains(key)) {
ConfigurationValueFlags valueFlags = sourceElement.Values.RetrieveFlags(key);
Values.SetValue(key, obj, valueFlags, null);
properties.Add(configurationProperty);
}
}
}
foreach (ConfigurationProperty property in Properties) {
if (property != null && (configurationPropertyCollection == null || configurationPropertyCollection.Contains(property.Name))) {
if (property.IsConfigurationElementType)
flag = true;
else {
object obj2 = sourceElement.Values[property.Name];
if ((property.IsRequired || saveMode == ConfigurationSaveMode.Full) && IsNullOrNullProperty(obj2) && property.DefaultValue != null)
obj2 = property.DefaultValue;
if (!IsNullOrNullProperty(obj2)) {
object obj3 = parentElement?.Values[property.Name] ?? property.DefaultValue;
switch (saveMode) {
case ConfigurationSaveMode.Minimal:
if (!object.Equals(obj2, obj3) || property.IsRequired)
Values[property.Name] = obj2;
break;
case ConfigurationSaveMode.Modified: {
bool flag2 = sourceElement.Values.IsModified(property.Name);
bool flag3 = sourceElement.Values.IsInherited(property.Name);
if ((property.IsRequired | flag2) || !flag3 || (((parentElement == null) & flag3) && !object.Equals(obj2, obj3)))
Values[property.Name] = obj2;
break;
}
case ConfigurationSaveMode.Full:
if (IsNullOrNullProperty(obj2))
Values[property.Name] = obj3;
else
Values[property.Name] = obj2;
break;
}
}
}
}
}
if (flag) {
foreach (ConfigurationProperty property2 in Properties) {
if (property2.IsConfigurationElementType) {
ConfigurationElement parentElement2 = (ConfigurationElement)(parentElement?[property2]);
ConfigurationElement configurationElement = (ConfigurationElement)this[property2];
if ((ConfigurationElement)sourceElement[property2] != null)
configurationElement.Unmerge((ConfigurationElement)sourceElement[property2], parentElement2, saveMode);
}
}
}
}
}
protected internal virtual bool SerializeToXmlElement(XmlWriter writer, string elementName)
{
if (_configRecord != null && _configRecord.TargetFramework != (FrameworkName)null) {
ConfigurationSection configurationSection = null;
if (_configRecord.SectionsStack.Count > 0)
configurationSection = (_configRecord.SectionsStack.Peek() as ConfigurationSection);
if (configurationSection != null && !configurationSection.ShouldSerializeElementInTargetVersion(this, elementName, _configRecord.TargetFramework))
return false;
}
bool flag = DataToWriteInternal;
if ((_lockedElementsList != null && _lockedElementsList.DefinedInParent(elementName)) || (_lockedAllExceptElementsList != null && _lockedAllExceptElementsList.HasParentElements && !_lockedAllExceptElementsList.DefinedInParent(elementName)))
return flag;
if (SerializeElement(null, false)) {
writer?.WriteStartElement(elementName);
flag |= SerializeElement(writer, false);
writer?.WriteEndElement();
}
return flag;
}
protected internal virtual bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
{
PreSerialize(writer);
bool flag = DataToWriteInternal;
bool flag2 = false;
bool flag3 = false;
ConfigurationPropertyCollection properties = Properties;
ConfigurationPropertyCollection configurationPropertyCollection = null;
for (int i = 0; i < Values.Count; i++) {
string key = Values.GetKey(i);
object obj = Values[i];
ConfigurationProperty configurationProperty = properties[key];
if (configurationProperty != null && (configurationPropertyCollection == null || configurationPropertyCollection.Contains(configurationProperty.Name))) {
if (configurationProperty.IsVersionCheckRequired && _configRecord != null && _configRecord.TargetFramework != (FrameworkName)null) {
ConfigurationSection configurationSection = null;
if (_configRecord.SectionsStack.Count > 0)
configurationSection = (_configRecord.SectionsStack.Peek() as ConfigurationSection);
if (configurationSection != null && !configurationSection.ShouldSerializePropertyInTargetVersion(configurationProperty, configurationProperty.Name, _configRecord.TargetFramework, this))
continue;
}
if (configurationProperty.IsConfigurationElementType)
flag2 = true;
else {
if ((_lockedAllExceptAttributesList != null && _lockedAllExceptAttributesList.HasParentElements && !_lockedAllExceptAttributesList.DefinedInParent(configurationProperty.Name)) || (_lockedAttributesList != null && _lockedAttributesList.DefinedInParent(configurationProperty.Name))) {
if (configurationProperty.IsRequired)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_required_attribute_locked, configurationProperty.Name));
obj = s_nullPropertyValue;
}
if (obj != s_nullPropertyValue && (!serializeCollectionKey || configurationProperty.IsKey)) {
string text;
if (obj is InvalidPropValue)
text = ((InvalidPropValue)obj).Value;
else {
configurationProperty.Validate(obj);
text = configurationProperty.ConvertToString(obj);
}
if (text != null && writer != null) {
if (configurationProperty.IsTypeStringTransformationRequired)
text = GetTransformedTypeString(text);
if (configurationProperty.IsAssemblyStringTransformationRequired)
text = GetTransformedAssemblyString(text);
writer.WriteAttributeString(configurationProperty.Name, text);
}
flag = (flag || text != null);
}
}
}
}
if (!serializeCollectionKey) {
flag |= SerializeLockList(_lockedAttributesList, "lockAttributes", writer);
flag |= SerializeLockList(_lockedAllExceptAttributesList, "lockAllAttributesExcept", writer);
flag |= SerializeLockList(_lockedElementsList, "lockElements", writer);
flag |= SerializeLockList(_lockedAllExceptElementsList, "lockAllElementsExcept", writer);
if ((_itemLockedFlag & ConfigurationValueFlags.Locked) != 0 && (_itemLockedFlag & ConfigurationValueFlags.Inherited) == ConfigurationValueFlags.Default && (_itemLockedFlag & ConfigurationValueFlags.XmlParentInherited) == ConfigurationValueFlags.Default) {
flag = true;
writer?.WriteAttributeString("lockItem", true.ToString().ToLowerInvariant());
}
}
if (flag2) {
for (int j = 0; j < Values.Count; j++) {
string key2 = Values.GetKey(j);
object obj2 = Values[j];
ConfigurationProperty configurationProperty2 = properties[key2];
if ((!serializeCollectionKey || configurationProperty2.IsKey) && obj2 is ConfigurationElement && (_lockedElementsList == null || !_lockedElementsList.DefinedInParent(key2)) && (_lockedAllExceptElementsList == null || !_lockedAllExceptElementsList.HasParentElements || _lockedAllExceptElementsList.DefinedInParent(key2))) {
ConfigurationElement configurationElement = (ConfigurationElement)obj2;
if (configurationProperty2.Name != "")
flag |= configurationElement.SerializeToXmlElement(writer, configurationProperty2.Name);
else {
if (flag3)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_element_cannot_have_multiple_child_elements, configurationProperty2.Name));
configurationElement._lockedAttributesList = null;
configurationElement._lockedAllExceptAttributesList = null;
configurationElement._lockedElementsList = null;
configurationElement._lockedAllExceptElementsList = null;
flag |= configurationElement.SerializeElement(writer, false);
flag3 = true;
}
}
}
}
return flag;
}
private bool SerializeLockList(ConfigurationLockCollection list, string elementKey, XmlWriter writer)
{
StringBuilder stringBuilder = new StringBuilder();
if (list != null) {
foreach (string item in list) {
if (!list.DefinedInParent(item)) {
if (stringBuilder.Length != 0)
stringBuilder.Append(',');
stringBuilder.Append(item);
}
}
}
if (writer != null && stringBuilder.Length != 0)
writer.WriteAttributeString(elementKey, stringBuilder.ToString());
return stringBuilder.Length != 0;
}
internal void ReportInvalidLock(string attribToLockTrim, ConfigurationLockCollectionType lockedType, ConfigurationValue value, string collectionProperties)
{
StringBuilder stringBuilder = new StringBuilder();
if (!string.IsNullOrEmpty(collectionProperties) && (lockedType == ConfigurationLockCollectionType.LockedElements || lockedType == ConfigurationLockCollectionType.LockedElementsExceptionList)) {
if (stringBuilder.Length != 0)
stringBuilder.Append(',');
stringBuilder.Append(collectionProperties);
}
foreach (object property in Properties) {
ConfigurationProperty configurationProperty = (ConfigurationProperty)property;
if (configurationProperty.Name != "lockAttributes" && configurationProperty.Name != "lockAllAttributesExcept" && configurationProperty.Name != "lockElements" && configurationProperty.Name != "lockAllElementsExcept") {
if (lockedType == ConfigurationLockCollectionType.LockedElements || lockedType == ConfigurationLockCollectionType.LockedElementsExceptionList) {
if (typeof(ConfigurationElement).IsAssignableFrom(configurationProperty.Type)) {
if (stringBuilder.Length != 0)
stringBuilder.Append(", ");
stringBuilder.Append('\'');
stringBuilder.Append(configurationProperty.Name);
stringBuilder.Append('\'');
}
} else if (!typeof(ConfigurationElement).IsAssignableFrom(configurationProperty.Type)) {
if (stringBuilder.Length != 0)
stringBuilder.Append(", ");
stringBuilder.Append('\'');
stringBuilder.Append(configurationProperty.Name);
stringBuilder.Append('\'');
}
}
}
string resourceFormat = (lockedType != ConfigurationLockCollectionType.LockedElements && lockedType != ConfigurationLockCollectionType.LockedElementsExceptionList) ? ((value != null) ? System.SR.Config_base_invalid_attribute_to_lock : System.SR.Config_base_invalid_attribute_to_lock_by_add) : ((value != null) ? System.SR.Config_base_invalid_element_to_lock : System.SR.Config_base_invalid_element_to_lock_by_add);
if (value != null)
throw new ConfigurationErrorsException(System.SR.Format(resourceFormat, attribToLockTrim, stringBuilder), value.SourceInfo.FileName, value.SourceInfo.LineNumber);
throw new ConfigurationErrorsException(System.SR.Format(resourceFormat, attribToLockTrim, stringBuilder));
}
private ConfigurationLockCollection ParseLockedAttributes(ConfigurationValue value, ConfigurationLockCollectionType lockType)
{
ConfigurationLockCollection configurationLockCollection = new ConfigurationLockCollection(this, lockType);
string text = (string)value.Value;
if (string.IsNullOrEmpty(text)) {
switch (lockType) {
case ConfigurationLockCollectionType.LockedAttributes:
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Empty_attribute, "lockAttributes"), value.SourceInfo.FileName, value.SourceInfo.LineNumber);
case ConfigurationLockCollectionType.LockedElements:
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Empty_attribute, "lockElements"), value.SourceInfo.FileName, value.SourceInfo.LineNumber);
case ConfigurationLockCollectionType.LockedExceptionList:
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_empty_lock_attributes_except, "lockAllAttributesExcept", "lockAttributes"), value.SourceInfo.FileName, value.SourceInfo.LineNumber);
case ConfigurationLockCollectionType.LockedElementsExceptionList:
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_empty_lock_element_except, "lockAllElementsExcept", "lockElements"), value.SourceInfo.FileName, value.SourceInfo.LineNumber);
}
}
string[] array = text.Split(',', ':', ';');
string[] array2 = array;
foreach (string text2 in array2) {
string text3 = text2.Trim();
if (!string.IsNullOrEmpty(text3)) {
if ((lockType != ConfigurationLockCollectionType.LockedElements && lockType != ConfigurationLockCollectionType.LockedAttributes) || !(text3 == "*")) {
ConfigurationProperty configurationProperty = Properties[text3];
if (configurationProperty == null || text3 == "lockAttributes" || text3 == "lockAllAttributesExcept" || text3 == "lockElements" || (lockType != ConfigurationLockCollectionType.LockedElements && lockType != ConfigurationLockCollectionType.LockedElementsExceptionList && typeof(ConfigurationElement).IsAssignableFrom(configurationProperty.Type)) || ((lockType == ConfigurationLockCollectionType.LockedElements || lockType == ConfigurationLockCollectionType.LockedElementsExceptionList) && !typeof(ConfigurationElement).IsAssignableFrom(configurationProperty.Type))) {
ConfigurationElementCollection configurationElementCollection = this as ConfigurationElementCollection;
if (configurationElementCollection == null && Properties.DefaultCollectionProperty != null)
configurationElementCollection = (this[Properties.DefaultCollectionProperty] as ConfigurationElementCollection);
if (configurationElementCollection == null || lockType == ConfigurationLockCollectionType.LockedAttributes || lockType == ConfigurationLockCollectionType.LockedExceptionList)
ReportInvalidLock(text3, lockType, value, null);
else if (!configurationElementCollection.IsLockableElement(text3)) {
ReportInvalidLock(text3, lockType, value, configurationElementCollection.LockableElements);
}
}
if (configurationProperty != null && configurationProperty.IsRequired)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_required_attribute_lock_attempt, configurationProperty.Name));
}
configurationLockCollection.Add(text3, ConfigurationValueFlags.Default);
}
}
return configurationLockCollection;
}
private StringCollection IntersectLockCollections(ConfigurationLockCollection collection1, ConfigurationLockCollection collection2)
{
ConfigurationLockCollection configurationLockCollection = (collection1.Count < collection2.Count) ? collection1 : collection2;
ConfigurationLockCollection configurationLockCollection2 = (collection1.Count >= collection2.Count) ? collection1 : collection2;
StringCollection stringCollection = new StringCollection();
foreach (string item in configurationLockCollection) {
if (configurationLockCollection2.Contains(item) || item == ElementTagName)
stringCollection.Add(item);
}
return stringCollection;
}
protected internal virtual void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
ConfigurationPropertyCollection properties = Properties;
ConfigurationValue configurationValue = null;
ConfigurationValue configurationValue2 = null;
ConfigurationValue configurationValue3 = null;
ConfigurationValue configurationValue4 = null;
bool flag = false;
ElementPresent = true;
ConfigurationElement configurationElement = null;
ConfigurationProperty configurationProperty = properties?.DefaultCollectionProperty;
if (configurationProperty != null)
configurationElement = (ConfigurationElement)this[configurationProperty];
ElementTagName = reader.Name;
PropertySourceInfo sourceInfo = new PropertySourceInfo(reader);
Values.SetValue(reader.Name, null, ConfigurationValueFlags.Modified, sourceInfo);
Values.SetValue("", configurationElement, ConfigurationValueFlags.Modified, sourceInfo);
if ((_lockedElementsList != null && (_lockedElementsList.Contains(reader.Name) || (_lockedElementsList.Contains("*") && reader.Name != ElementTagName))) || (_lockedAllExceptElementsList != null && _lockedAllExceptElementsList.Count != 0 && !_lockedAllExceptElementsList.Contains(reader.Name)) || ((_itemLockedFlag & ConfigurationValueFlags.Locked) != 0 && (_itemLockedFlag & ConfigurationValueFlags.Inherited) != 0))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_element_locked, reader.Name), reader);
if (reader.AttributeCount > 0) {
while (reader.MoveToNextAttribute()) {
string name = reader.Name;
if (((_lockedAttributesList != null && (_lockedAttributesList.Contains(name) || _lockedAttributesList.Contains("*"))) || (_lockedAllExceptAttributesList != null && !_lockedAllExceptAttributesList.Contains(name))) && name != "lockAttributes" && name != "lockAllAttributesExcept")
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_attribute_locked, name), reader);
ConfigurationProperty configurationProperty2 = properties?[name];
if (configurationProperty2 != null) {
if (serializeCollectionKey && !configurationProperty2.IsKey)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_unrecognized_attribute, name), reader);
Values.SetValue(name, DeserializePropertyValue(configurationProperty2, reader), ConfigurationValueFlags.Modified, new PropertySourceInfo(reader));
} else if (!(name == "lockItem")) {
if (!(name == "lockAttributes")) {
if (!(name == "lockAllAttributesExcept")) {
if (!(name == "lockElements")) {
if (name == "lockAllElementsExcept")
configurationValue4 = new ConfigurationValue(reader.Value, ConfigurationValueFlags.Default, new PropertySourceInfo(reader));
else if (serializeCollectionKey || !OnDeserializeUnrecognizedAttribute(name, reader.Value)) {
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_unrecognized_attribute, name), reader);
}
} else
configurationValue3 = new ConfigurationValue(reader.Value, ConfigurationValueFlags.Default, new PropertySourceInfo(reader));
} else
configurationValue2 = new ConfigurationValue(reader.Value, ConfigurationValueFlags.Default, new PropertySourceInfo(reader));
} else
configurationValue = new ConfigurationValue(reader.Value, ConfigurationValueFlags.Default, new PropertySourceInfo(reader));
} else {
try {
flag = bool.Parse(reader.Value);
} catch {
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_invalid_boolean_attribute, name), reader);
}
}
}
}
reader.MoveToElement();
try {
HybridDictionary hybridDictionary = new HybridDictionary();
if (!reader.IsEmptyElement) {
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
string name2 = reader.Name;
CheckLockedElement(name2, null);
ConfigurationProperty configurationProperty3 = properties?[name2];
if (configurationProperty3 != null) {
if (!configurationProperty3.IsConfigurationElementType)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_property_is_not_a_configuration_element, name2), reader);
if (hybridDictionary.Contains(name2))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_element_cannot_have_multiple_child_elements, name2), reader);
hybridDictionary.Add(name2, name2);
ConfigurationElement configurationElement2 = (ConfigurationElement)this[configurationProperty3];
configurationElement2.DeserializeElement(reader, serializeCollectionKey);
ValidateElement(configurationElement2, configurationProperty3.Validator, false);
} else if (!OnDeserializeUnrecognizedElement(name2, reader) && (configurationElement == null || !configurationElement.OnDeserializeUnrecognizedElement(name2, reader))) {
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_unrecognized_element_name, name2), reader);
}
} else {
if (reader.NodeType == XmlNodeType.EndElement)
break;
if (reader.NodeType == XmlNodeType.CDATA || reader.NodeType == XmlNodeType.Text)
throw new ConfigurationErrorsException(System.SR.Config_base_section_invalid_content, reader);
}
}
}
EnsureRequiredProperties(serializeCollectionKey);
ValidateElement(this, null, false);
} catch (ConfigurationException ex) {
if (ex.Filename == null || ex.Filename.Length == 0)
throw new ConfigurationErrorsException(ex.Message, reader);
throw;
}
if (flag) {
SetLocked();
_itemLockedFlag = ConfigurationValueFlags.Locked;
}
if (configurationValue != null) {
if (_lockedAttributesList == null)
_lockedAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedAttributes);
foreach (string item in ParseLockedAttributes(configurationValue, ConfigurationLockCollectionType.LockedAttributes)) {
if (!_lockedAttributesList.Contains(item))
_lockedAttributesList.Add(item, ConfigurationValueFlags.Default);
else
_lockedAttributesList.Add(item, ConfigurationValueFlags.Inherited | ConfigurationValueFlags.Modified);
}
}
if (configurationValue2 != null) {
ConfigurationLockCollection configurationLockCollection = ParseLockedAttributes(configurationValue2, ConfigurationLockCollectionType.LockedExceptionList);
if (_lockedAllExceptAttributesList == null) {
_lockedAllExceptAttributesList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedExceptionList, string.Empty, configurationLockCollection);
_lockedAllExceptAttributesList.ClearSeedList();
}
StringCollection stringCollection = IntersectLockCollections(_lockedAllExceptAttributesList, configurationLockCollection);
_lockedAllExceptAttributesList.ClearInternal(false);
StringEnumerator enumerator2 = stringCollection.GetEnumerator();
try {
while (enumerator2.MoveNext()) {
string current = enumerator2.Current;
_lockedAllExceptAttributesList.Add(current, ConfigurationValueFlags.Default);
}
} finally {
(enumerator2 as IDisposable)?.Dispose();
}
}
if (configurationValue3 != null) {
if (_lockedElementsList == null)
_lockedElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElements);
ConfigurationLockCollection configurationLockCollection2 = ParseLockedAttributes(configurationValue3, ConfigurationLockCollectionType.LockedElements);
if (properties.DefaultCollectionProperty != null) {
ConfigurationElementCollection configurationElementCollection = this[properties.DefaultCollectionProperty] as ConfigurationElementCollection;
if (configurationElementCollection != null && configurationElementCollection._lockedElementsList == null)
configurationElementCollection._lockedElementsList = _lockedElementsList;
}
foreach (string item2 in configurationLockCollection2) {
if (!_lockedElementsList.Contains(item2)) {
_lockedElementsList.Add(item2, ConfigurationValueFlags.Default);
ConfigurationProperty configurationProperty4 = Properties[item2];
if (configurationProperty4 != null && typeof(ConfigurationElement).IsAssignableFrom(configurationProperty4.Type))
((ConfigurationElement)this[item2]).SetLocked();
if (item2 == "*") {
foreach (ConfigurationProperty property in Properties) {
if (!string.IsNullOrEmpty(property.Name) && property.IsConfigurationElementType)
((ConfigurationElement)this[property]).SetLocked();
}
}
}
}
}
if (configurationValue4 != null) {
ConfigurationLockCollection configurationLockCollection3 = ParseLockedAttributes(configurationValue4, ConfigurationLockCollectionType.LockedElementsExceptionList);
if (_lockedAllExceptElementsList == null) {
_lockedAllExceptElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElementsExceptionList, ElementTagName, configurationLockCollection3);
_lockedAllExceptElementsList.ClearSeedList();
}
StringCollection stringCollection2 = IntersectLockCollections(_lockedAllExceptElementsList, configurationLockCollection3);
if (properties.DefaultCollectionProperty != null) {
ConfigurationElementCollection configurationElementCollection2 = this[properties.DefaultCollectionProperty] as ConfigurationElementCollection;
if (configurationElementCollection2 != null && configurationElementCollection2._lockedAllExceptElementsList == null)
configurationElementCollection2._lockedAllExceptElementsList = _lockedAllExceptElementsList;
}
_lockedAllExceptElementsList.ClearInternal(false);
StringEnumerator enumerator5 = stringCollection2.GetEnumerator();
try {
while (enumerator5.MoveNext()) {
string current2 = enumerator5.Current;
if (!_lockedAllExceptElementsList.Contains(current2) || current2 == ElementTagName)
_lockedAllExceptElementsList.Add(current2, ConfigurationValueFlags.Default);
}
} finally {
(enumerator5 as IDisposable)?.Dispose();
}
foreach (ConfigurationProperty property2 in Properties) {
if (!string.IsNullOrEmpty(property2.Name) && !_lockedAllExceptElementsList.Contains(property2.Name) && property2.IsConfigurationElementType)
((ConfigurationElement)this[property2]).SetLocked();
}
}
if (configurationProperty != null) {
configurationElement = (ConfigurationElement)this[configurationProperty];
if (_lockedElementsList == null)
_lockedElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElements);
configurationElement._lockedElementsList = _lockedElementsList;
if (_lockedAllExceptElementsList == null) {
_lockedAllExceptElementsList = new ConfigurationLockCollection(this, ConfigurationLockCollectionType.LockedElementsExceptionList, reader.Name);
_lockedAllExceptElementsList.ClearSeedList();
}
configurationElement._lockedAllExceptElementsList = _lockedAllExceptElementsList;
}
PostDeserialize();
}
private object DeserializePropertyValue(ConfigurationProperty prop, XmlReader reader)
{
string value = reader.Value;
object obj = null;
try {
obj = prop.ConvertFromString(value);
prop.Validate(obj);
return obj;
} catch (ConfigurationException ex) {
ConfigurationException ex2 = ex;
if (string.IsNullOrEmpty(ex2.Filename))
ex2 = new ConfigurationErrorsException(ex2.Message, reader);
return new InvalidPropValue(value, ex2);
} catch {
return obj;
}
}
internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive)
{
ConfigurationValidatorBase configurationValidatorBase = propValidator;
if (configurationValidatorBase == null && elem.ElementProperty != null) {
configurationValidatorBase = elem.ElementProperty.Validator;
if (configurationValidatorBase != null && !configurationValidatorBase.CanValidate(elem.GetType()))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Validator_does_not_support_elem_type, elem.GetType().Name));
}
try {
configurationValidatorBase?.Validate(elem);
} catch (ConfigurationException) {
throw;
} catch (Exception ex2) {
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Validator_element_not_valid, elem.ElementTagName, ex2.Message));
}
if (recursive) {
if (elem is ConfigurationElementCollection) {
IEnumerator elementsEnumerator = ((ConfigurationElementCollection)elem).GetElementsEnumerator();
while (elementsEnumerator.MoveNext()) {
ValidateElement((ConfigurationElement)elementsEnumerator.Current, null, true);
}
}
for (int i = 0; i < elem.Values.Count; i++) {
ConfigurationElement configurationElement = elem.Values[i] as ConfigurationElement;
if (configurationElement != null)
ValidateElement(configurationElement, null, true);
}
}
}
private void EnsureRequiredProperties(bool ensureKeysOnly)
{
ConfigurationPropertyCollection properties = Properties;
if (properties != null) {
foreach (ConfigurationProperty item in properties) {
if (item.IsRequired && !Values.Contains(item.Name) && (!ensureKeysOnly || item.IsKey))
Values[item.Name] = OnRequiredPropertyNotFound(item.Name);
}
}
}
protected virtual object OnRequiredPropertyNotFound(string name)
{
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_required_attribute_missing, name), PropertyFileName(name), PropertyLineNumber(name));
}
protected virtual void PostDeserialize()
{
}
protected virtual void PreSerialize(XmlWriter writer)
{
}
protected virtual bool OnDeserializeUnrecognizedAttribute(string name, string value)
{
return false;
}
protected virtual bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
{
return false;
}
protected virtual string GetTransformedTypeString(string typeName)
{
if (typeName == null || _configRecord == null || !_configRecord.TypeStringTransformerIsSet)
return typeName;
return _configRecord.TypeStringTransformer(typeName);
}
protected virtual string GetTransformedAssemblyString(string assemblyName)
{
if (assemblyName == null || _configRecord == null || !_configRecord.AssemblyStringTransformerIsSet)
return assemblyName;
return _configRecord.AssemblyStringTransformer(assemblyName);
}
internal ConfigurationLockCollection UnMergeLockList(ConfigurationLockCollection sourceLockList, ConfigurationLockCollection parentLockList, ConfigurationSaveMode saveMode)
{
if (!sourceLockList.ExceptionList) {
switch (saveMode) {
case ConfigurationSaveMode.Modified: {
ConfigurationLockCollection configurationLockCollection2 = new ConfigurationLockCollection(this, sourceLockList.LockType);
{
foreach (string sourceLock in sourceLockList) {
if (!parentLockList.Contains(sourceLock) || sourceLockList.IsValueModified(sourceLock))
configurationLockCollection2.Add(sourceLock, ConfigurationValueFlags.Default);
}
return configurationLockCollection2;
}
}
case ConfigurationSaveMode.Minimal: {
ConfigurationLockCollection configurationLockCollection = new ConfigurationLockCollection(this, sourceLockList.LockType);
{
foreach (string sourceLock2 in sourceLockList) {
if (!parentLockList.Contains(sourceLock2))
configurationLockCollection.Add(sourceLock2, ConfigurationValueFlags.Default);
}
return configurationLockCollection;
}
}
}
} else if (saveMode == ConfigurationSaveMode.Modified || saveMode == ConfigurationSaveMode.Minimal) {
bool flag = false;
if (sourceLockList.Count == parentLockList.Count) {
flag = true;
foreach (string sourceLock3 in sourceLockList) {
if (!parentLockList.Contains(sourceLock3) || (sourceLockList.IsValueModified(sourceLock3) && saveMode == ConfigurationSaveMode.Modified))
flag = false;
}
}
if (flag)
return null;
}
return sourceLockList;
}
internal static bool IsLockAttributeName(string name)
{
if (!StringUtil.StartsWithOrdinal(name, "lock"))
return false;
string[] array = s_lockAttributeNames;
foreach (string b in array) {
if (name == b)
return true;
}
return false;
}
}
}