Exemplo n.º 1
0
        /// <summary>
        /// Gets the <see cref="CategorizedSettingsElementCollection"/> object representing settings under the specified category name.
        /// </summary>
        /// <param name="name">Name of the category whose settings are to be retrieved.</param>
        /// <returns><see cref="CategorizedSettingsElementCollection"/> object with settings under the specified category name.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> is null or empty string.</exception>
        public new CategorizedSettingsElementCollection this[string name]
        {
            get
            {
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException("name");
                }

                // We will add the requested category to the default properties collection, so that when
                // the settings are saved off to the config file, all of the categories under which
                // settings may be saved are processed and saved to the config file. This is essentially
                // doing what marking a property with the <ConfigurationProperty()> attribute does.
                // Make the first letter of category name lower case, if not already.
                char[] nameChars = name.ToCharArray();
                nameChars[0] = char.ToLower(nameChars[0]);

                // Do not allow spaces in the name so that underlying .Net configuration API does not break.
                name = (new string(nameChars)).RemoveWhiteSpace();

                ConfigurationProperty configProperty = new ConfigurationProperty(name, typeof(CategorizedSettingsElementCollection));
                CategorizedSettingsElementCollection settingsCategory = null;

                if ((object)m_sections == null)
                {
                    base.Properties.Add(configProperty);

                    if ((object)base[configProperty] != null)
                    {
                        settingsCategory         = (CategorizedSettingsElementCollection) base[configProperty];
                        settingsCategory.Name    = name;
                        settingsCategory.Section = this;
                        settingsCategory.SetCryptoKey(m_cryptoKey);
                    }
                }
                else
                {
                    settingsCategory = m_sections.GetOrAdd(name, sectionName =>
                    {
                        base.Properties.Add(configProperty);

                        CategorizedSettingsElementCollection settings = new CategorizedSettingsElementCollection
                        {
                            Name    = sectionName,
                            Section = this,
                        };

                        settings.SetCryptoKey(m_cryptoKey);
                        Modified = true;

                        return(settings);
                    });
                }

                return(settingsCategory);
            }
        }
        /// <summary>
        /// Reads XML from the configuration file.
        /// </summary>
        /// <param name="reader">The <see cref="System.Xml.XmlReader"/> object, which reads from the configuration file.</param>
        protected override void DeserializeSection(XmlReader reader)
        {
            using (BlockAllocatedMemoryStream configSectionStream = new BlockAllocatedMemoryStream())
            {
                XmlDocument configSection = new XmlDocument();

                configSection.Load(reader);
                configSection.Save(configSectionStream);

                // Adds all the categories that are under the categorizedSettings section of the configuration file
                // to the property collection. Again, this is essentially doing what marking a property with the
                // <ConfigurationProperty()> attribute does. If this is not done, then an exception will be raised
                // when the category elements are being deserialized.
                if ((object)configSection.DocumentElement != null)
                {
                    XmlNodeList categories = configSection.DocumentElement.SelectNodes("*");

                    if ((object)categories != null)
                    {
                        foreach (XmlNode category in categories)
                        {
                            ConfigurationProperty configProperty = new ConfigurationProperty(category.Name, typeof(CategorizedSettingsElementCollection));

                            base.Properties.Add(configProperty);

                            if ((object)m_sections != null)
                            {
                                CategorizedSettingsElementCollection settingsCategory = new CategorizedSettingsElementCollection
                                {
                                    Name = category.Name,
                                    Section = this,
                                };

                                settingsCategory.SetCryptoKey(m_cryptoKey);
                                m_sections.Add(category.Name, settingsCategory);

                                // Read all elements within this category section
                                XmlNodeList elements = category.SelectNodes("*");
                                SettingScope scope;

                                if ((object)elements != null)
                                {
                                    foreach (XmlNode element in elements)
                                    {
                                        CategorizedSettingsElement categorySetting = new CategorizedSettingsElement(settingsCategory);

                                        categorySetting.Name = element.GetAttributeValue("name");
                                        categorySetting.Value = element.GetAttributeValue("value");
                                        categorySetting.Description = element.GetAttributeValue("description") ?? "";
                                        categorySetting.Encrypted = element.GetAttributeValue("encrypted").ToNonNullNorWhiteSpace("false").ParseBoolean();

                                        if (Enum.TryParse(element.GetAttributeValue("scope").ToNonNullNorWhiteSpace("Application"), out scope))
                                            categorySetting.Scope = scope;
                                        else
                                            categorySetting.Scope = SettingScope.Application;

                                        settingsCategory.Add(categorySetting);
                                    }
                                }
                            }
                        }
                    }
                }

                m_sectionLoaded = true;

                if ((object)m_sections == null)
                {
                    configSectionStream.Seek(0, SeekOrigin.Begin);
                    base.DeserializeSection(XmlReader.Create(configSectionStream));
                }
            }
        }
        /// <summary>
        /// Gets the <see cref="CategorizedSettingsElementCollection"/> object representing settings under the specified category name.
        /// </summary>
        /// <param name="name">Name of the category whose settings are to be retrieved.</param>
        /// <returns><see cref="CategorizedSettingsElementCollection"/> object with settings under the specified category name.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> is null or empty string.</exception>
        public new CategorizedSettingsElementCollection this[string name]
        {
            get
            {
                if (string.IsNullOrEmpty(name))
                    throw new ArgumentNullException(nameof(name));

                // We will add the requested category to the default properties collection, so that when
                // the settings are saved off to the config file, all of the categories under which
                // settings may be saved are processed and saved to the config file. This is essentially
                // doing what marking a property with the <ConfigurationProperty()> attribute does.
                // Make the first letter of category name lower case, if not already.
                char[] nameChars = name.ToCharArray();
                nameChars[0] = char.ToLower(nameChars[0]);

                // Do not allow spaces in the name so that underlying .Net configuration API does not break.
                name = (new string(nameChars)).RemoveWhiteSpace();

                ConfigurationProperty configProperty = new ConfigurationProperty(name, typeof(CategorizedSettingsElementCollection));
                CategorizedSettingsElementCollection settingsCategory = null;

                if ((object)m_sections == null)
                {
                    base.Properties.Add(configProperty);

                    if ((object)base[configProperty] != null)
                    {
                        settingsCategory = (CategorizedSettingsElementCollection)base[configProperty];
                        settingsCategory.Name = name;
                        settingsCategory.Section = this;
                        settingsCategory.SetCryptoKey(m_cryptoKey);
                    }
                }
                else
                {
                    settingsCategory = m_sections.GetOrAdd(name, sectionName =>
                    {
                        base.Properties.Add(configProperty);

                        CategorizedSettingsElementCollection settings = new CategorizedSettingsElementCollection
                        {
                            Name = sectionName,
                            Section = this,
                        };

                        settings.SetCryptoKey(m_cryptoKey);
                        Modified = true;

                        return settings;
                    });
                }

                return settingsCategory;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads XML from the configuration file.
        /// </summary>
        /// <param name="reader">The <see cref="System.Xml.XmlReader"/> object, which reads from the configuration file.</param>
        protected override void DeserializeSection(XmlReader reader)
        {
            using (BlockAllocatedMemoryStream configSectionStream = new BlockAllocatedMemoryStream())
            {
                XmlDocument configSection = new XmlDocument();

                configSection.Load(reader);
                configSection.Save(configSectionStream);

                // Adds all the categories that are under the categorizedSettings section of the configuration file
                // to the property collection. Again, this is essentially doing what marking a property with the
                // <ConfigurationProperty()> attribute does. If this is not done, then an exception will be raised
                // when the category elements are being deserialized.
                if ((object)configSection.DocumentElement != null)
                {
                    XmlNodeList categories = configSection.DocumentElement.SelectNodes("*");

                    if ((object)categories != null)
                    {
                        foreach (XmlNode category in categories)
                        {
                            ConfigurationProperty configProperty = new ConfigurationProperty(category.Name, typeof(CategorizedSettingsElementCollection));

                            base.Properties.Add(configProperty);

                            if ((object)m_sections != null)
                            {
                                CategorizedSettingsElementCollection settingsCategory = new CategorizedSettingsElementCollection
                                {
                                    Name    = category.Name,
                                    Section = this,
                                };

                                settingsCategory.SetCryptoKey(m_cryptoKey);
                                m_sections.Add(category.Name, settingsCategory);

                                // Read all elements within this category section
                                XmlNodeList  elements = category.SelectNodes("*");
                                SettingScope scope;

                                if ((object)elements != null)
                                {
                                    foreach (XmlNode element in elements)
                                    {
                                        CategorizedSettingsElement categorySetting = new CategorizedSettingsElement(settingsCategory);

                                        categorySetting.Name        = element.GetAttributeValue("name");
                                        categorySetting.Value       = element.GetAttributeValue("value");
                                        categorySetting.Description = element.GetAttributeValue("description") ?? "";
                                        categorySetting.Encrypted   = element.GetAttributeValue("encrypted").ToNonNullNorWhiteSpace("false").ParseBoolean();

                                        if (Enum.TryParse(element.GetAttributeValue("scope").ToNonNullNorWhiteSpace("Application"), out scope))
                                        {
                                            categorySetting.Scope = scope;
                                        }
                                        else
                                        {
                                            categorySetting.Scope = SettingScope.Application;
                                        }

                                        settingsCategory.Add(categorySetting);
                                    }
                                }
                            }
                        }
                    }
                }

                m_sectionLoaded = true;

                if ((object)m_sections == null)
                {
                    configSectionStream.Seek(0, SeekOrigin.Begin);
                    base.DeserializeSection(XmlReader.Create(configSectionStream));
                }
            }
        }