Get() public method

public Get ( string elementKey ) : SettingElement
elementKey string
return SettingElement
Beispiel #1
0
		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);
        }
        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);
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        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);
            }
        }
Beispiel #6
0
        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>
        /// 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;
            }
        }
        /// <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);
                }
            }
        }
Beispiel #9
0
		public void CollectionGetNonExistent ()
		{
			SettingElementCollection c = new SettingElementCollection ();
			Assert.IsNull (c.Get ("nonexistent"));
		}