mscorlib(4.0.0.0) API with additions
SettingsBase.cs
2 
3 namespace System.Configuration
4 {
6  public abstract class SettingsBase
7  {
8  private SettingsPropertyCollection _Properties;
9 
10  private SettingsProviderCollection _Providers;
11 
12  private SettingsPropertyValueCollection _PropertyValues;
13 
14  private SettingsContext _Context;
15 
16  private bool _IsSynchronized;
17 
24  public virtual object this[string propertyName]
25  {
26  get
27  {
28  if (IsSynchronized)
29  {
30  lock (this)
31  {
32  return GetPropertyValueByName(propertyName);
33  }
34  }
35  return GetPropertyValueByName(propertyName);
36  }
37  set
38  {
39  if (IsSynchronized)
40  {
41  lock (this)
42  {
43  SetPropertyValueByName(propertyName, value);
44  }
45  }
46  else
47  {
48  SetPropertyValueByName(propertyName, value);
49  }
50  }
51  }
52 
55  public virtual SettingsPropertyCollection Properties => _Properties;
56 
59  public virtual SettingsProviderCollection Providers => _Providers;
60 
63  public virtual SettingsPropertyValueCollection PropertyValues => _PropertyValues;
64 
67  public virtual SettingsContext Context => _Context;
68 
72  [Browsable(false)]
73  public bool IsSynchronized
74  {
75  get
76  {
77  return _IsSynchronized;
78  }
79  }
80 
82  protected SettingsBase()
83  {
84  _PropertyValues = new SettingsPropertyValueCollection();
85  }
86 
87  private object GetPropertyValueByName(string propertyName)
88  {
89  if (Properties == null || _PropertyValues == null || Properties.Count == 0)
90  {
91  throw new SettingsPropertyNotFoundException(System.SR.GetString("SettingsPropertyNotFound", propertyName));
92  }
93  SettingsProperty settingsProperty = Properties[propertyName];
94  if (settingsProperty == null)
95  {
96  throw new SettingsPropertyNotFoundException(System.SR.GetString("SettingsPropertyNotFound", propertyName));
97  }
98  SettingsPropertyValue settingsPropertyValue = _PropertyValues[propertyName];
99  if (settingsPropertyValue == null)
100  {
101  GetPropertiesFromProvider(settingsProperty.Provider);
102  settingsPropertyValue = _PropertyValues[propertyName];
103  if (settingsPropertyValue == null)
104  {
105  throw new SettingsPropertyNotFoundException(System.SR.GetString("SettingsPropertyNotFound", propertyName));
106  }
107  }
108  return settingsPropertyValue.PropertyValue;
109  }
110 
111  private void SetPropertyValueByName(string propertyName, object propertyValue)
112  {
113  if (Properties == null || _PropertyValues == null || Properties.Count == 0)
114  {
115  throw new SettingsPropertyNotFoundException(System.SR.GetString("SettingsPropertyNotFound", propertyName));
116  }
117  SettingsProperty settingsProperty = Properties[propertyName];
118  if (settingsProperty == null)
119  {
120  throw new SettingsPropertyNotFoundException(System.SR.GetString("SettingsPropertyNotFound", propertyName));
121  }
122  if (settingsProperty.IsReadOnly)
123  {
124  throw new SettingsPropertyIsReadOnlyException(System.SR.GetString("SettingsPropertyReadOnly", propertyName));
125  }
126  if (propertyValue != null && !settingsProperty.PropertyType.IsInstanceOfType(propertyValue))
127  {
128  throw new SettingsPropertyWrongTypeException(System.SR.GetString("SettingsPropertyWrongType", propertyName));
129  }
130  SettingsPropertyValue settingsPropertyValue = _PropertyValues[propertyName];
131  if (settingsPropertyValue == null)
132  {
133  GetPropertiesFromProvider(settingsProperty.Provider);
134  settingsPropertyValue = _PropertyValues[propertyName];
135  if (settingsPropertyValue == null)
136  {
137  throw new SettingsPropertyNotFoundException(System.SR.GetString("SettingsPropertyNotFound", propertyName));
138  }
139  }
140  settingsPropertyValue.PropertyValue = propertyValue;
141  }
142 
148  {
149  _Context = context;
150  _Properties = properties;
151  _Providers = providers;
152  }
153 
155  public virtual void Save()
156  {
157  if (IsSynchronized)
158  {
159  lock (this)
160  {
161  SaveCore();
162  }
163  }
164  else
165  {
166  SaveCore();
167  }
168  }
169 
170  private void SaveCore()
171  {
172  if (Properties != null && _PropertyValues != null && Properties.Count != 0)
173  {
174  foreach (SettingsProvider provider in Providers)
175  {
176  SettingsPropertyValueCollection settingsPropertyValueCollection = new SettingsPropertyValueCollection();
177  foreach (SettingsPropertyValue propertyValue in PropertyValues)
178  {
179  if (propertyValue.Property.Provider == provider)
180  {
181  settingsPropertyValueCollection.Add(propertyValue);
182  }
183  }
184  if (settingsPropertyValueCollection.Count > 0)
185  {
186  provider.SetPropertyValues(Context, settingsPropertyValueCollection);
187  }
188  }
189  foreach (SettingsPropertyValue propertyValue2 in PropertyValues)
190  {
191  propertyValue2.IsDirty = false;
192  }
193  }
194  }
195 
196  private void GetPropertiesFromProvider(SettingsProvider provider)
197  {
198  SettingsPropertyCollection settingsPropertyCollection = new SettingsPropertyCollection();
199  foreach (SettingsProperty property in Properties)
200  {
201  if (property.Provider == provider)
202  {
203  settingsPropertyCollection.Add(property);
204  }
205  }
206  if (settingsPropertyCollection.Count > 0)
207  {
208  SettingsPropertyValueCollection propertyValues = provider.GetPropertyValues(Context, settingsPropertyCollection);
209  foreach (SettingsPropertyValue item in propertyValues)
210  {
211  if (_PropertyValues[item.Name] == null)
212  {
213  _PropertyValues.Add(item);
214  }
215  }
216  }
217  }
218 
222  public static SettingsBase Synchronized(SettingsBase settingsBase)
223  {
224  settingsBase._IsSynchronized = true;
225  return settingsBase;
226  }
227  }
228 }
abstract void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
Sets the values of the specified group of property settings.
virtual SettingsProviderCollection Providers
Gets a collection of settings providers.
Definition: SettingsBase.cs:59
int Count
Gets a value that specifies the number of T:System.Configuration.SettingsPropertyValue objects in the...
Definition: __Canon.cs:3
Provides the base class used to support user property settings.
Definition: SettingsBase.cs:6
virtual SettingsProvider Provider
Gets or sets the provider for the T:System.Configuration.SettingsProperty.
Contains a collection of settings property values that map T:System.Configuration....
Contains a collection of T:System.Configuration.SettingsProperty objects.
SettingsProperty Property
Gets the T:System.Configuration.SettingsProperty object.
virtual void Save()
Stores the current values of the settings properties.
Represents a collection of application settings providers.
bool IsSynchronized
Gets a value indicating whether access to the object is synchronized (thread safe).
Definition: SettingsBase.cs:74
Definition: SR.cs:7
void Add(SettingsPropertyValue property)
Adds a T:System.Configuration.SettingsPropertyValue object to the collection.
void Initialize(SettingsContext context, SettingsPropertyCollection properties, SettingsProviderCollection providers)
Initializes internal properties used by T:System.Configuration.SettingsBase object.
virtual SettingsPropertyCollection Properties
Gets the collection of settings properties.
Definition: SettingsBase.cs:55
Contains the value of a settings property that can be loaded and stored by an instance of T:System....
static SettingsBase Synchronized(SettingsBase settingsBase)
Provides a T:System.Configuration.SettingsBase class that is synchronized (thread safe).
Provides contextual information that the provider can use when persisting settings.
virtual SettingsPropertyValueCollection PropertyValues
Gets a collection of settings property values.
Definition: SettingsBase.cs:63
SettingsBase()
Initializes a new instance of the T:System.Configuration.SettingsBase class.
Definition: SettingsBase.cs:82
Acts as a base class for deriving custom settings providers in the application settings architecture.
int Count
Gets a value that specifies the number of T:System.Configuration.SettingsProperty objects in the coll...
virtual SettingsContext Context
Gets the associated settings context.
Definition: SettingsBase.cs:67
Provides an exception for T:System.Configuration.SettingsProperty objects that are not found.