Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 获取与指定名称关联的配置容器的值。
 /// </summary>
 /// <param name="name">配置容器的名称。</param>
 /// <param name="value">配置容器的值。</param>
 /// <returns></returns>
 /// <exception cref="Exception"/>
 public bool TryGetValue(string name, out CustumSection value)
 {
     if (_values.TryGetValue(name, out IConfigSection val))
     {
         value = (CustumSection)val;
         return(true);
     }
     else
     {
         value = null;
         return(false);
     }
 }
Ejemplo n.º 3
0
        internal ConfigSectionSet(XElement declarationSuperior, XElement contentSuperior, ISavable savable)
        {
            _declarationSuperior = declarationSuperior;
            _contentSuperior     = contentSuperior;
            _savable             = savable;
            if (declarationSuperior.HasElements)
            {
                foreach (XElement declaration in declarationSuperior.Elements("section"))
                {
                    string         name     = declaration.Attribute("name").Value;
                    string         typeName = declaration.Attribute("type").Value;
                    XElement       content  = contentSuperior.Element(name);
                    IConfigSection value;
                    switch (typeName)
                    {
                    case "System.Configuration.DictionarySectionHandler, System":
                    case "System.Configuration.DictionarySectionHandler":
                    case "DictionarySectionHandler":
                        value = new DictionarySection(content, savable);
                        break;

                    case "System.Configuration.NameValueSectionHandler, System":
                    case "System.Configuration.NameValueSectionHandler":
                    case "NameValueSectionHandler":
                        value = new NameValueSection(content, savable);
                        break;

                    case "System.Configuration.SingleTagSectionHandler, System":
                    case "System.Configuration.SingleTagSectionHandler":
                    case "SingleTagSectionHandler":
                        value = new SingleTagSection(content, savable);
                        break;

                    case "LH.Configuration.CustumSectionHandler":
                    case "CustumSectionHandler":
                    default: value = new CustumSection(content, savable); break;
                    }
                    _values.Add(name, value);
                    _contents.Add(name, content);
                    _declarations.Add(name, declaration);
                }
            }
        }