Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Sys / System / Configuration / SettingsBase.cs / 1 / SettingsBase.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Configuration { using System.Collections.Specialized; using System.Runtime.Serialization; using System.Configuration.Provider; using System.Collections; using System.ComponentModel; //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// public abstract class SettingsBase { protected SettingsBase() { _PropertyValues = new SettingsPropertyValueCollection(); } //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// public virtual object this[string propertyName] { get { if (IsSynchronized) { lock (this) { return GetPropertyValueByName(propertyName); } } else { return GetPropertyValueByName(propertyName); } } set { if (IsSynchronized) { lock (this) { SetPropertyValueByName(propertyName, value); } } else { SetPropertyValueByName(propertyName, value); } } } private object GetPropertyValueByName(string propertyName) { if (Properties == null || _PropertyValues == null || Properties.Count == 0) throw new SettingsPropertyNotFoundException(SR.GetString(SR.SettingsPropertyNotFound, propertyName)); SettingsProperty pp = Properties[propertyName]; if (pp == null) throw new SettingsPropertyNotFoundException(SR.GetString(SR.SettingsPropertyNotFound, propertyName)); SettingsPropertyValue p = _PropertyValues[propertyName]; if (p == null) { GetPropertiesFromProvider(pp.Provider); p = _PropertyValues[propertyName]; if (p == null) throw new SettingsPropertyNotFoundException(SR.GetString(SR.SettingsPropertyNotFound, propertyName)); } return p.PropertyValue; } private void SetPropertyValueByName(string propertyName, object propertyValue) { if (Properties == null || _PropertyValues == null || Properties.Count == 0) throw new SettingsPropertyNotFoundException(SR.GetString(SR.SettingsPropertyNotFound, propertyName)); SettingsProperty pp = Properties[propertyName]; if (pp == null) throw new SettingsPropertyNotFoundException(SR.GetString(SR.SettingsPropertyNotFound, propertyName)); if (pp.IsReadOnly) throw new SettingsPropertyIsReadOnlyException(SR.GetString(SR.SettingsPropertyReadOnly, propertyName)); if (propertyValue != null && !pp.PropertyType.IsInstanceOfType(propertyValue)) throw new SettingsPropertyWrongTypeException(SR.GetString(SR.SettingsPropertyWrongType, propertyName)); SettingsPropertyValue p = _PropertyValues[propertyName]; if (p == null) { GetPropertiesFromProvider(pp.Provider); p = _PropertyValues[propertyName]; if (p == null) throw new SettingsPropertyNotFoundException(SR.GetString(SR.SettingsPropertyNotFound, propertyName)); } p.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) return; foreach(SettingsProvider prov in Providers) { SettingsPropertyValueCollection ppcv = new SettingsPropertyValueCollection(); foreach (SettingsPropertyValue pp in PropertyValues) { if (pp.Property.Provider == prov) { ppcv.Add(pp); } } if (ppcv.Count > 0) { prov.SetPropertyValues(Context, ppcv); } } foreach (SettingsPropertyValue pp in PropertyValues) pp.IsDirty = false; } virtual public SettingsPropertyCollection Properties { get { return _Properties; }} virtual public SettingsProviderCollection Providers { get { return _Providers; }} virtual public SettingsPropertyValueCollection PropertyValues { get { return _PropertyValues; } } virtual public SettingsContext Context { get { return _Context; } } private void GetPropertiesFromProvider(SettingsProvider provider) { SettingsPropertyCollection ppc = new SettingsPropertyCollection(); foreach (SettingsProperty pp in Properties) { if (pp.Provider == provider) { ppc.Add(pp); } } if (ppc.Count > 0) { SettingsPropertyValueCollection ppcv = provider.GetPropertyValues(Context, ppc); foreach (SettingsPropertyValue p in ppcv) { if (_PropertyValues[p.Name] == null) _PropertyValues.Add(p); } } } public static SettingsBase Synchronized(SettingsBase settingsBase) { settingsBase._IsSynchronized = true; return settingsBase; } //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// private SettingsPropertyCollection _Properties = null; private SettingsProviderCollection _Providers = null; private SettingsPropertyValueCollection _PropertyValues = null; private SettingsContext _Context = null; private bool _IsSynchronized = false; [Browsable(false)] public bool IsSynchronized { get { return _IsSynchronized; } } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AssemblyCache.cs
- PackageDigitalSignatureManager.cs
- FileClassifier.cs
- SqlProfileProvider.cs
- XPathQilFactory.cs
- PolicyUnit.cs
- _OSSOCK.cs
- Vector3dCollection.cs
- ProfileParameter.cs
- LocatorPart.cs
- GuidTagList.cs
- RegexCaptureCollection.cs
- Positioning.cs
- compensatingcollection.cs
- RadioButton.cs
- COM2ColorConverter.cs
- DSASignatureFormatter.cs
- ControlFilterExpression.cs
- StateItem.cs
- QilStrConcatenator.cs
- DateTimeFormatInfo.cs
- AutoResizedEvent.cs
- GetPageCompletedEventArgs.cs
- ObjectComplexPropertyMapping.cs
- ADRole.cs
- StrongBox.cs
- DynamicPhysicalDiscoSearcher.cs
- HeaderedContentControl.cs
- PropertyInfo.cs
- UpdatePanelControlTrigger.cs
- KeyValuePairs.cs
- CmsInterop.cs
- HtmlLabelAdapter.cs
- HwndSourceParameters.cs
- FontNamesConverter.cs
- TreeView.cs
- PartialCachingControl.cs
- EntityClassGenerator.cs
- HGlobalSafeHandle.cs
- GridViewUpdateEventArgs.cs
- FrameworkPropertyMetadata.cs
- DesignerActionList.cs
- PopupEventArgs.cs
- HostingEnvironment.cs
- EdmMember.cs
- InternalConfigHost.cs
- ScrollChangedEventArgs.cs
- ScriptingAuthenticationServiceSection.cs
- TextTreeNode.cs
- SharedUtils.cs
- CalendarDay.cs
- ObjectSpanRewriter.cs
- EventDrivenDesigner.cs
- ApplicationHost.cs
- RowUpdatedEventArgs.cs
- XappLauncher.cs
- Publisher.cs
- DataGridColumn.cs
- TempEnvironment.cs
- RectAnimationBase.cs
- TextTreeUndoUnit.cs
- SchemaImporterExtensionElement.cs
- DateBoldEvent.cs
- WebBrowserNavigatedEventHandler.cs
- ListSortDescription.cs
- OpenTypeLayout.cs
- BasicCellRelation.cs
- HtmlTableCellCollection.cs
- SafeArrayRankMismatchException.cs
- cookiecontainer.cs
- IResourceProvider.cs
- FileNotFoundException.cs
- ProxyDataContractResolver.cs
- EditorPartChrome.cs
- ScopeElement.cs
- ApplyImportsAction.cs
- AsyncResult.cs
- XmlUtf8RawTextWriter.cs
- LOSFormatter.cs
- RuntimeIdentifierPropertyAttribute.cs
- Rotation3DKeyFrameCollection.cs
- SqlPersonalizationProvider.cs
- DataSetUtil.cs
- MenuItemStyleCollection.cs
- ClientProxyGenerator.cs
- XsltOutput.cs
- CachedTypeface.cs
- StatusBar.cs
- ReadOnlyDataSourceView.cs
- Propagator.cs
- RegexBoyerMoore.cs
- DataGridItemAttachedStorage.cs
- PatternMatcher.cs
- ApplyTemplatesAction.cs
- DesignerView.cs
- TextAction.cs
- OpenTypeLayoutCache.cs
- View.cs
- GeneralTransform3DCollection.cs
- FrameworkElementFactoryMarkupObject.cs