Exemple #1
0
        /// <summary>
        /// 获取与指定名称关联的配置容器的值。如果不存在,添加一个配置容器并返回值。不能使用此方法获取或添加 CustumSection 类型。
        /// </summary>
        /// <param name="name">配置容器的名称。</param>
        /// <param name="type">配置容器的类型。</param>
        /// <exception cref="Exception"/>
        public IConfigSection GetOrAdd(string name, ConfigSectionType type)
        {
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (name.Contains(" "))
            {
                throw new ArgumentException(ExceptionMessages.InvalidKey.Message + " - " + nameof(name));
            }
            if (_values.ContainsKey(name))
            {
                return(_values[name]);
            }
            else
            {
                XElement declaration = new XElement("section");
                declaration.SetAttributeValue("name", name);
                XElement       content = new XElement(name);
                IConfigSection value;
                switch (type)
                {
                case ConfigSectionType.CustumSection:
                    declaration.SetAttributeValue("type", "LH.Configuration.CustumSectionHandler");
                    value = new CustumSection(content, _savable);
                    break;

                case ConfigSectionType.DictionarySection:
                    declaration.SetAttributeValue("type", "System.Configuration.DictionarySectionHandler");
                    value = new DictionarySection(content, _savable);
                    break;

                case ConfigSectionType.NameValueSection:
                    declaration.SetAttributeValue("type", "System.Configuration.NameValueSectionHandler");
                    value = new NameValueSection(content, _savable);
                    break;

                case ConfigSectionType.SingleTagSection:
                    declaration.SetAttributeValue("type", "System.Configuration.SingleTagSectionHandler");
                    value = new SingleTagSection(content, _savable);
                    break;

                default: throw new ArgumentException(ExceptionMessages.InvalidType.Message + " - " + nameof(type));
                }
                _values.Add(name, value);
                _contents.Add(name, content);
                _contentSuperior.Add(content);
                _declarations.Add(name, declaration);
                _declarationSuperior.Add(declaration);
                if (_savable.AutoSave)
                {
                    _savable.Save();
                }
                return(value);
            }
        }
Exemple #2
0
        public void Should_throw_if_not_default()
        {
            var config = BuildConfig();

            var autoUpdateProperty = ConfigSectionType.GetProperty("AutoUpdate");

            autoUpdateProperty.SetValue(config, true);

            var ex = Assert.Throws <NotSupportedException>(() => { Validate(config); });

            Assert.True(ex.Message.Contains("AutoUpdate"));
        }