<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0-preview.2.23128.3" />

SettingsBase

public abstract class SettingsBase
using System.ComponentModel; namespace System.Configuration { public abstract class SettingsBase { private SettingsPropertyCollection _properties; private SettingsProviderCollection _providers; private readonly SettingsPropertyValueCollection _propertyValues; private SettingsContext _context; private bool _isSynchronized; public virtual object this[string propertyName] { get { if (IsSynchronized) { lock (this) { return GetPropertyValueByName(propertyName); } } return GetPropertyValueByName(propertyName); } set { if (IsSynchronized) { lock (this) { SetPropertyValueByName(propertyName, value); } } else SetPropertyValueByName(propertyName, value); } } public virtual SettingsPropertyCollection Properties => _properties; public virtual SettingsProviderCollection Providers => _providers; public virtual SettingsPropertyValueCollection PropertyValues => _propertyValues; public virtual SettingsContext Context => _context; [Browsable(false)] public bool IsSynchronized { get { return _isSynchronized; } } protected SettingsBase() { _propertyValues = new SettingsPropertyValueCollection(); } private object GetPropertyValueByName(string propertyName) { if (Properties == null || _propertyValues == null || Properties.Count == 0) throw new SettingsPropertyNotFoundException(System.SR.Format(System.SR.SettingsPropertyNotFound, propertyName)); SettingsProperty settingsProperty = Properties[propertyName]; if (settingsProperty == null) throw new SettingsPropertyNotFoundException(System.SR.Format(System.SR.SettingsPropertyNotFound, propertyName)); SettingsPropertyValue settingsPropertyValue = _propertyValues[propertyName]; if (settingsPropertyValue == null) { GetPropertiesFromProvider(settingsProperty.Provider); settingsPropertyValue = _propertyValues[propertyName]; if (settingsPropertyValue == null) throw new SettingsPropertyNotFoundException(System.SR.Format(System.SR.SettingsPropertyNotFound, propertyName)); } return settingsPropertyValue.PropertyValue; } private void SetPropertyValueByName(string propertyName, object propertyValue) { if (Properties == null || _propertyValues == null || Properties.Count == 0) throw new SettingsPropertyNotFoundException(System.SR.Format(System.SR.SettingsPropertyNotFound, propertyName)); SettingsProperty settingsProperty = Properties[propertyName]; if (settingsProperty == null) throw new SettingsPropertyNotFoundException(System.SR.Format(System.SR.SettingsPropertyNotFound, propertyName)); if (settingsProperty.IsReadOnly) throw new SettingsPropertyIsReadOnlyException(System.SR.Format(System.SR.SettingsPropertyReadOnly, propertyName)); if (propertyValue != null && !settingsProperty.PropertyType.IsInstanceOfType(propertyValue)) throw new SettingsPropertyWrongTypeException(System.SR.Format(System.SR.SettingsPropertyWrongType, propertyName)); SettingsPropertyValue settingsPropertyValue = _propertyValues[propertyName]; if (settingsPropertyValue == null) { GetPropertiesFromProvider(settingsProperty.Provider); settingsPropertyValue = _propertyValues[propertyName]; if (settingsPropertyValue == null) throw new SettingsPropertyNotFoundException(System.SR.Format(System.SR.SettingsPropertyNotFound, propertyName)); } settingsPropertyValue.PropertyValue = propertyValue; } public void Initialize(SettingsContext context, SettingsPropertyCollection properties, SettingsProviderCollection providers) { _context = context; _properties = properties; _providers = providers; } public virtual void Save() { if (IsSynchronized) { lock (this) { SaveCore(); } } else SaveCore(); } private void SaveCore() { if (Properties != null && _propertyValues != null && Properties.Count != 0) { foreach (SettingsProvider provider in Providers) { SettingsPropertyValueCollection settingsPropertyValueCollection = new SettingsPropertyValueCollection(); foreach (SettingsPropertyValue propertyValue in PropertyValues) { if (propertyValue.Property.Provider == provider) settingsPropertyValueCollection.Add(propertyValue); } if (settingsPropertyValueCollection.Count > 0) provider.SetPropertyValues(Context, settingsPropertyValueCollection); } foreach (SettingsPropertyValue propertyValue2 in PropertyValues) { propertyValue2.IsDirty = false; } } } private void GetPropertiesFromProvider(SettingsProvider provider) { SettingsPropertyCollection settingsPropertyCollection = new SettingsPropertyCollection(); foreach (SettingsProperty property in Properties) { if (property.Provider == provider) settingsPropertyCollection.Add(property); } if (settingsPropertyCollection.Count > 0) { SettingsPropertyValueCollection propertyValues = provider.GetPropertyValues(Context, settingsPropertyCollection); foreach (SettingsPropertyValue item in propertyValues) { if (_propertyValues[item.Name] == null) _propertyValues.Add(item); } } } public static SettingsBase Synchronized(SettingsBase settingsBase) { settingsBase._isSynchronized = true; return settingsBase; } } }