c# - 如何从 C# 中的 app.config 获取一些元素?

标签 c# c#-4.0 app-config configurationmanager

根据我的 app.config,我希望能够获取列的元素,如以下示例中第 3-5 行所示。我应该如何编写代码才能实现这一点。如果我需要更改我的 app.config - 我会这样做。

1. var bounceProviders = ConfigurationManager.GetSection("BounceProviders") as BounceProvidersSection;
2. var providerColumns = bounceProviders.Providers[0].Columns;
3. var emailColumn = providerColumns.Email;
4. var dateColumn = providerColumns.Date;
5. var messageColumn = providerColumns.Message;

app.config

<BounceProviders>
  <Providers>
    <Provider Name="p1">        
      <Columns>
          <Email Name="My Email" />
          <Date Name="My Date" />
          <Message Name="My Message" />               
      </Columns>
    </Provider>  
    <Provider Name="p2">
      <Columns />
    </Provider>    
  </Providers>
</BounceProviders>

配置部分

public class BounceProvidersSection : ConfigurationSection
{
    [ConfigurationCollection(typeof(ConfigCollection<BounceProviderConfig>), AddItemName = "Provider")]
    [ConfigurationProperty("Providers", IsRequired = true)]
    public ConfigCollection<BounceProviderConfig> Providers
    {
        get { return (ConfigCollection<BounceProviderConfig>)this["Providers"]; }
        set { this["Providers"] = value; }
    }                
}

public class BounceProviderConfig : ConfigurationElement
{
    [ConfigurationProperty("Name", IsRequired = true)]
    public string Name
    {
        get { return (string)this["Name"]; }
        set { this["Name"] = value; }
    }            
}

配置集合

public class ConfigCollection<T> : ConfigurationElementCollection
        where T : ConfigurationElement, new()
{        
    protected override ConfigurationElement CreateNewElement()
    {
        return new T();
    }

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

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

    new public T this[string Name]
    {
        get
        {
            return ( T )BaseGet( Name );
        }
    }
}

自卫队

最佳答案

我想通了

public class BounceProvidersSection : ConfigurationSection
{
    [ConfigurationCollection(typeof(ConfigCollection<BounceProviderConfig>), AddItemName = "Provider")]
    [ConfigurationProperty("Providers", IsRequired = true)]
    public ConfigCollection<BounceProviderConfig> Providers
    {
        get { return (ConfigCollection<BounceProviderConfig>)this["Providers"]; }
        set { this["Providers"] = value; }
    }                
}

public class BounceProviderConfig : ConfigurationElement
{
    [ConfigurationProperty("Id", IsRequired = true)]
    public int Id
    {
        get { return (int)this["Id"]; }
        set { this["Id"] = value; }
    }

    [ConfigurationProperty("Columns", IsRequired = false)]
    public BounceProviderColumns Columns
    {
        get { return (BounceProviderColumns)this["Columns"]; }
        set { this["Columns"] = value; }
    }

    public static BounceProviderConfig GetByProviderId(int providerId)
    {
        var section = ConfigUtils.GetConfigurationSection<BounceProvidersSection>("BounceProviders");
        foreach (BounceProviderConfig provider in section.Providers)
        {
            if (provider.Id == providerId)
                return provider;
        }

        return null;
    }
}

public class BounceProviderColumns : ConfigurationElement
{
    [ConfigurationProperty("Email", IsRequired = true)]
    public ColumnConfig Email
    {
        get { return (ColumnConfig)this["Email"]; }
        set { this["Email"] = value; }
    }

    [ConfigurationProperty("Date", IsRequired = true)]
    public DateColumnConfig Date
    {
        get { return (DateColumnConfig)this["Date"]; }
        set { this["Date"] = value; }
    }

    [ConfigurationProperty("Message", IsRequired = true)]
    public ColumnConfig Message
    {
        get { return (ColumnConfig)this["Message"]; }
        set { this["Message"] = value; }
    }        
}

public class ColumnConfig : ConfigurationElement
{
    [ConfigurationProperty("Name", IsRequired = true)]
    public string Name
    {
        get { return (string)this["Name"]; }
        set { this["Name"] = value; }
    }
}

关于c# - 如何从 C# 中的 app.config 获取一些元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11773575/

相关文章:

Linq,获取按字段分组的不同元素

c# - Spring.net,AOP 导致无法设置属性

wcf - 在以编程方式构造的端点上声明性地配置 WCF 行为扩展

.net - Windows 服务的 Log4net

c# - UserPrincipal.FindByIdentity 权限

c# - 是否有用于 Xml 的 Json2Csharp.com?

c# - 动态创建 Func<> - Lambda 与表达式树

有 Web 引用时的 .NET DLL 设置和配置 - 发生了什么?

c# - 多个搜索框

c# - C#中 bool 值的大小是多少?它真的需要 4 个字节吗?