public void CollectionAddNameless () { SettingElement el = new SettingElement (); Assert.AreEqual (String.Empty, el.Name, "premise #1"); SettingElementCollection c = new SettingElementCollection (); Assert.AreEqual (ConfigurationElementCollectionType.BasicMap, c.CollectionType, "premise #2"); c.Add (el); Assert.AreEqual (el, c.Get (""), "#1"); }
public void SuccessfulRetrieveSettingElement() { var settingElement = new SettingElement("TestElementName", SettingsSerializeAs.String); var collection = new SettingElementCollection(); collection.Add(settingElement); var retrievedElement = collection.Get("TestElementName"); Assert.Same(settingElement, retrievedElement); }
/// <summary> /// Checks the specified settings collection to see if it has a serialized value that should /// be applied to the specified <see cref="SettingsPropertyValue"/>. /// </summary> /// <param name="value">An individual settings property value.</param> /// <param name="settings">A collection representing the settings.</param> private static void ApplySettingToValue(SettingsPropertyValue value, SettingElementCollection settings) { var setting = settings.Get(value.Name); if (setting != null) { value.SerializedValue = setting.Value.ValueXml.InnerText; // Mark the value as not deserialized, which will trigger a deserialization of the SerializedValue into the PropertyValue. value.Deserialized = false; } }
public void ClearNoElementsLeft() { var settingElement = new SettingElement("TestElementName", SettingsSerializeAs.String); var collection = new SettingElementCollection(); collection.Add(settingElement); collection.Clear(); var retrievedElement = collection.Get("TestElementName"); Assert.Null(retrievedElement); }
public void CollectionAddNull () { try { SettingElementCollection c = new SettingElementCollection (); c.Add (null); Assert.Fail (); } catch (NullReferenceException) { // .net s cks here } catch (ArgumentNullException) { } }
public void SuccessfullyRemoveElement() { var settingElement = new SettingElement("TestElementName", SettingsSerializeAs.String); var collection = new SettingElementCollection(); collection.Add(settingElement); collection.Remove(settingElement); var retrievedElement = collection.Get("TestElementName"); Assert.Equal(null, retrievedElement); }
internal static void WriteSettings(string sectionName, bool isRoaming, IDictionary newSettings) { if (!ConfigurationManagerInternalFactory.Instance.SupportsUserConfig) { throw new ConfigurationErrorsException(SR.UserSettingsNotSupported); } Configuration config = GetUserConfig(isRoaming); ClientSettingsSection section = GetConfigSection(config, sectionName, true); if (section != null) { SettingElementCollection sec = section.Settings; foreach (DictionaryEntry entry in newSettings) { SettingElement se = sec.Get((string)entry.Key); if (se == null) { se = new SettingElement(); se.Name = (string)entry.Key; sec.Add(se); } StoredSetting ss = (StoredSetting)entry.Value; se.SerializeAs = ss.SerializeAs; se.Value.ValueXml = ss.Value; } try { config.Save(); } catch (ConfigurationErrorsException ex) { // We wrap this in an exception with our error message and throw again. throw new ConfigurationErrorsException(SR.Format(SR.SettingsSaveFailed, ex.Message), ex); } } else { throw new ConfigurationErrorsException(SR.SettingsSaveFailedNoSection); } }
internal void WriteSettings(string sectionName, bool isRoaming, IDictionary newSettings) { if (!ConfigurationManagerInternalFactory.Instance.SupportsUserConfig) { throw new ConfigurationErrorsException(System.SR.GetString("UserSettingsNotSupported")); } System.Configuration.Configuration userConfig = this.GetUserConfig(isRoaming); ClientSettingsSection section = this.GetConfigSection(userConfig, sectionName, true); if (section != null) { SettingElementCollection settings = section.Settings; foreach (DictionaryEntry entry in newSettings) { SettingElement element = settings.Get((string)entry.Key); if (element == null) { element = new SettingElement { Name = (string)entry.Key }; settings.Add(element); } StoredSetting setting = (StoredSetting)entry.Value; element.SerializeAs = setting.SerializeAs; element.Value.ValueXml = setting.Value; } try { userConfig.Save(); return; } catch (ConfigurationErrorsException exception) { throw new ConfigurationErrorsException(System.SR.GetString("SettingsSaveFailed", new object[] { exception.Message }), exception); } } throw new ConfigurationErrorsException(System.SR.GetString("SettingsSaveFailedNoSection")); }
/// <summary> /// Updates the <see cref="SettingElementCollection"/> from the <see cref="SettingsPropertyValueCollection"/>. /// </summary> /// <param name="settings">A collection representing the settings.</param> /// <param name="values"> /// A <see cref="T:System.Configuration.SettingsPropertyValueCollection"/> representing the /// group of property settings to set. /// </param> private static void UpdateSettingsFromPropertyValues(SettingElementCollection settings, SettingsPropertyValueCollection values) { foreach (SettingsPropertyValue value in values) { if (value.IsDirty) { var element = settings.Get(value.Name); if (element == null) { // Note: We only support string serialization for brevity of implementation. element = new SettingElement(value.Name, SettingsSerializeAs.String); settings.Add(element); } element.SerializeAs = SettingsSerializeAs.String; element.Value.ValueXml = CreateXmlValue(value.SerializedValue); } } }
/// <summary> /// Merges the specified user and solution settings into a new <see cref="SettingsPropertyValueCollection"/>. /// </summary> /// <param name="userSettings">The user settings.</param> /// <param name="solutionSettings">The solution settings.</param> /// <param name="properties">The setting properties collection.</param> /// <returns>A merged <see cref="SettingsPropertyValueCollection"/>.</returns> private static SettingsPropertyValueCollection MergeSettingsIntoPropertyValues(SettingElementCollection userSettings, SettingElementCollection solutionSettings, SettingsPropertyCollection properties) { var values = new SettingsPropertyValueCollection(); foreach (SettingsProperty property in properties) { var value = new SettingsPropertyValue(property); ApplySettingToValue(value, userSettings); ApplySettingToValue(value, solutionSettings); value.IsDirty = false; values.Add(value); } return values; }
public SettingElementCollectionWrapper(SettingElementCollection settings) { _settings = settings; }
public void DefaultCollectionTypeBasicMap() { var collection = new SettingElementCollection(); Assert.Equal(ConfigurationElementCollectionType.BasicMap, collection.CollectionType); }
public void CollectionGetNonExistent () { SettingElementCollection c = new SettingElementCollection (); Assert.IsNull (c.Get ("nonexistent")); }