2012-08-02 61 views
2

根據我的app.config我希望能夠得到列的元素,如下面的示例3-5行所示。我應該如何編寫代碼才能實現這一目標。如果我需要更改我的app.config - 我會做。如何從C#中的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> 

的ConfigurationSection

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; } 
    }    
} 

ConfigCollection

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); 
     } 
    } 
} 

自衛隊

+0

一張小紙條:'返回element.GetHashCode()':這是不正確的,散列碼並不打算是唯一的。你的代碼可能會在這裏出現異常。 – Dennis 2012-08-02 08:34:06

回答

1

我想通了

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; } 
    } 
} 
相關問題