按名称查找 app.config 个元素列表

Lookup app.config element list by name

我想根据名称从我的 app.config 中检索设置。

  <jobs>
    <add name="Name" sftpName="sftp" jobName="jobName"/>
    <add name="Name2" sftpName="sftp2" jobName="jobName2"/>
  </jobs>

我希望能够按名字查找。

我已经为工作创建了一个 ConfigurationSection,但我似乎无法让它根据名称给我名称、sftpName 和 jobName。

这将在之后反序列化为 class。

有没有办法在 .NET 中自动生成一些东西?

实现此目的的方法是拥有一个集合:

using System.Configuration;

namespace SerialApp
{
    public class JobSection : ConfigurationSection
    {
        [ConfigurationProperty("jobs", IsDefaultCollection = true)]
        public JobCollection Jobs
        {
            get
            {
                JobCollection hostCollection = (JobCollection)base["jobs"];
                return hostCollection;
            }
        }
    }

    public class JobCollection : ConfigurationElementCollection
    {
        public new JobConfigElement this[string name]
        {
            get
            {
                if (IndexOf(name) < 0) return null;
                return (JobConfigElement)BaseGet(name);
            }
        }
        public JobConfigElement this[int index]
        {
            get { return (JobConfigElement)BaseGet(index); }
        }
        public int IndexOf(string name)
        {
            name = name.ToLower();

            for (int idx = 0; idx < base.Count; idx++)
            {
                if (this[idx].Name.ToLower() == name)
                    return idx;
            }
            return -1;
        }
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new JobConfigElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((JobConfigElement)element).Name;
        }
        protected override string ElementName
        {
            get { return "job"; }
        }
    }

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

        [ConfigurationProperty("name", DefaultValue = "Name")]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }
        [ConfigurationProperty("sftpName", DefaultValue = "sftp")]
        public string SftpName
        {
            get { return (string)this["sftpName"]; }
            set { this["sftpName"] = value; }
        }
        [ConfigurationProperty("jobName", DefaultValue = "jobName")]
        public string JobName
        {
            get { return (string)this["jobName"]; }
            set { this["jobName"] = value; }
        }
    }
}

在 .config 中添加以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="JobSection" type="SerialApp.JobSection, SerialApp" />
  </configSections>
  <JobSection>
    <jobs>
      <job name="Name" sftpName="sftp" jobName="jobName"/>
      <job name="Name2" sftpName="sftp2" jobName="jobName2"/>
    </jobs>
  </JobSection>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
</configuration>

并简称为:

JobSection jobs = ConfigurationManager.GetSection("JobSection") as JobSection; 
if (jobSection != null)
{
    JobCollection jobCollection = jobSection.Jobs;
    var job = jobCollection["Name"];
    var jobName = job.JobName;
}