c# - 如何使用 ConfigurationElementCollection 实现 ConfigurationSection

标签 c# configuration app-config configuration-files

我正在尝试在项目中实现自定义配置部分,但我一直遇到我不理解的异常。我希望有人可以填补这里的空白。

我的 App.config 看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="ServicesSection" type="RT.Core.Config.ServicesConfigurationSectionHandler, RT.Core"/>
    </configSections>
    <ServicesSection type="RT.Core.Config.ServicesSection, RT.Core">
            <Services>
                <AddService Port="6996" ReportType="File" />
                <AddService Port="7001" ReportType="Other" />
            </Services>
        </ServicesSection>
</configuration>

我有一个定义如下的 ServiceConfig 元素:

public class ServiceConfig : ConfigurationElement
  {
    public ServiceConfig() {}

    public ServiceConfig(int port, string reportType)
    {
      Port = port;
      ReportType = reportType;
    }

    [ConfigurationProperty("Port", DefaultValue = 0, IsRequired = true, IsKey = true)]
    public int Port 
    {
      get { return (int) this["Port"]; }
      set { this["Port"] = value; }
    }

    [ConfigurationProperty("ReportType", DefaultValue = "File", IsRequired = true, IsKey = false)]
    public string ReportType
    {
      get { return (string) this["ReportType"]; }
      set { this["ReportType"] = value; }
    }
  }

我有一个 ServiceCollection 定义如下:

public class ServiceCollection : ConfigurationElementCollection
  {
    public ServiceCollection()
    {
      Console.WriteLine("ServiceCollection Constructor");
    }

    public ServiceConfig this[int index]
    {
      get { return (ServiceConfig)BaseGet(index); }
      set
      {
        if (BaseGet(index) != null)
        {
          BaseRemoveAt(index);
        }
        BaseAdd(index, value);
      }
    }

    public void Add(ServiceConfig serviceConfig)
    {
      BaseAdd(serviceConfig);
    }

    public void Clear()
    {
      BaseClear();
    }

    protected override ConfigurationElement CreateNewElement()
    {
      return new ServiceConfig();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
      return ((ServiceConfig) element).Port;
    }

    public void Remove(ServiceConfig serviceConfig)
    {
      BaseRemove(serviceConfig.Port);
    }

    public void RemoveAt(int index)
    {
      BaseRemoveAt(index);
    }

    public void Remove(string name)
    {
      BaseRemove(name);
    }
  }

我缺少的部分是为处理程序做什么。最初,我尝试实现一个 IConfigurationSectionHandler 但发现了两件事:

  1. 没用
  2. 它已被弃用。

我现在完全不知道该怎么做才能从配置中读取我的数据。请帮忙!

最佳答案

前面的答案是正确的,但我也会给你所有的代码。

您的 app.config 应如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
      <section name="ServicesSection" type="RT.Core.Config.ServiceConfigurationSection, RT.Core"/>
   </configSections>
   <ServicesSection>
      <Services>
         <add Port="6996" ReportType="File" />
         <add Port="7001" ReportType="Other" />
      </Services>
   </ServicesSection>
</configuration>

您的 ServiceConfigServiceCollection 类保持不变。

你需要一个新类:

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

这应该可以解决问题。要使用它,您可以使用:

ServiceConfigurationSection serviceConfigSection =
   ConfigurationManager.GetSection("ServicesSection") as ServiceConfigurationSection;

ServiceConfig serviceConfig = serviceConfigSection.Services[0];

关于c# - 如何使用 ConfigurationElementCollection 实现 ConfigurationSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3935331/

相关文章:

c# - 你如何在对象上实现 GetHashCode()?

c# - 类方法构建

entity-framework - 在 F# 中,什么时候是 "constant string expression"而不是 "constant string expression"

c# - 如何在asp.net中的代码后面的html按钮中获取隐藏字段值

vim - 如何避免在 vim 启动时出现 "Added cscope database"

email - Grails邮件插件运行时配置

sql - Postgresql:如何确保索引在内存中

c# - 在 Windows 窗体应用程序中处理连接字符串的最佳方法是什么?

c# - 如何在 app.config 中存储用户可调整的配置?

c# - 获取错误 Error-Server tags cannot contain <% ... %> constructs