1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
//------------------------------------------------------------------------------
// <copyright file="SettingsBase.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
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; } }
}
}
|