使用配置元素集合 c# 实现自定义节

本文关键字:实现 自定义 集合 配置 元素 | 更新日期: 2023-09-27 18:36:35

我想在项目中实现自定义配置部分。但是有些我不明白,所以不起作用。

我有看起来像这样的应用程序配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="DepartmentConfigurationSection" type="Statistics.Config.DepartmentSection , Program1"/>
  </configSections>
  <s>
    <Cash>
      <add Number="1" Name="Money" />
    </Cash>
    <Departments>
      <add Id="1" Name="x" />
      <add Id="2" Name="y" />
    </Departments>
  </s>
</configuration>

我创建了一个名为 DepartmentSection 的文件.cs它与 ConfigurationElement、ConfigurationElementCollection 和 ConfigurationSection 相吻合。类是这样的:

 public class DepartmentConfig : ConfigurationElement
    {
        public DepartmentConfig() { }
        public DepartmentConfig(int id, string name)
        {
            Id = id;
            Name = name;
        }
        [ConfigurationProperty("Id", IsRequired = true, IsKey = true)]
        public int Id
        {
            get { return (int)this["Id"]; }
            set { this["Id"] = value; }
        }
        [ConfigurationProperty("Name",  IsRequired = true, IsKey = false)]
        public string Name
        {
            get { return (string)this["Name"]; }
            set { this["Name"] = value; }
        }
    }

    public class DepartmentCollection : ConfigurationElementCollection
    {
        public DepartmentCollection()
        {
            Console.WriteLine("ServiceCollection Constructor");
        }
        public DepartmentConfig this[int index]
        {
            get { return (DepartmentConfig)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
        public void Add(DepartmentConfig depConfig)
        {
            BaseAdd(depConfig);
        }
        public void Clear()
        {
            BaseClear();
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new DepartmentConfig();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DepartmentConfig)element).Id;
        }
        public void Remove(DepartmentConfig depConfig)
        {
            BaseRemove(depConfig.Id);
        }
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
        public void Remove(string name)
        {
            BaseRemove(name);
        }
    }

    public class DepartmentConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("Departments", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(DepartmentCollection),
            AddItemName = "add",
            ClearItemsName = "clear",
            RemoveItemName = "remove")]
        public DepartmentCollection Departments
        {
            get
            {
                return (DepartmentCollection)base["Departments"];
            }
        }
    }

我试图从处理程序获取集合,但没有成功。我尝试过这样,但给我这个错误:"无法初始化系统配置"。

    DepartmentConfigurationSection serviceConfigSection =
    ConfigurationManager.GetSection("s") as DepartmentConfigurationSection;
    DepartmentConfig serviceConfig = serviceConfigSection.Departments[0];

使用配置元素集合 c# 实现自定义节

问题似乎出在您的app.config(或web.config)中。包含自定义配置 XML 的元素必须与您在 configSections'sectionname 属性中指定的名称匹配。例如,为了使您的代码按编写的方式工作,app.config 应如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="s" type="Statistics.Config.DepartmentConfigurationSection, Program1"/>
  </configSections>
  <s>
    <Cash>
      <add Number="1" Name="Money" />
    </Cash>
    <Departments>
      <add Id="1" Name="x" />
      <add Id="2" Name="y" />
    </Departments>
  </s>
</configuration>

如您所见,section name="s"s元素的名称匹配。此外,您将类型列为 Statistics.Config.DeptartmentSection ,但您的类名是 DepartmentConfigurationSection ,因此它应该与您尝试加载的类匹配。

我认为您需要在 DepartmentCollection 类中添加类级属性:

[DepartmentCollection(typeof(DepartmentConfig ), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap)]