Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C#中的app.config获取一些元素?_C#_C# 4.0_App Config_Configurationmanager - Fatal编程技术网

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

如何从C#中的app.config获取一些元素?,c#,c#-4.0,app-config,configurationmanager,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.

基于我的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 );
        }
    }
}
公共类BounceProviders部分:配置部分
{
[ConfigurationCollection(typeof(ConfigCollection),AddItemName=“Provider”)]
[配置属性(“提供程序”,IsRequired=true)]
公共集合提供程序
{
获取{return(ConfigCollection)this[“Providers”];}
设置{this[“Providers”]=value;}
}                
}
公共类BounceProviderConfig:ConfigurationElement
{
[配置属性(“名称”,IsRequired=true)]
公共字符串名
{
获取{return(string)this[“Name”];}
设置{this[“Name”]=value;}
}            
}
ConfigCollection

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 );
        }
    }
}
公共类ConfigCollection:ConfigurationElementCollection
其中T:ConfigurationElement,new()
{        
受保护的覆盖ConfigurationElement CreateNewElement()
{
返回新的T();
}
受保护的覆盖对象GetElementKey(ConfigurationElement元素)
{
返回元素。GetHashCode();
}
公共T此[int索引]
{
得到
{
返回(T)BaseGet(索引);
}
设置
{
if(BaseGet(index)!=null)
{
BaseRemoveAt(索引);
}
BaseAdd(索引、值);
}
}
此[字符串名称]
{
得到
{
return(T)BaseGet(Name);
}
}
}
sdf

我想出来了

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; }
    }
}
公共类BounceProviders部分:配置部分
{
[ConfigurationCollection(typeof(ConfigCollection),AddItemName=“Provider”)]
[配置属性(“提供程序”,IsRequired=true)]
公共集合提供程序
{
获取{return(ConfigCollection)this[“Providers”];}
设置{this[“Providers”]=value;}
}                
}
公共类BounceProviderConfig:ConfigurationElement
{
[ConfigurationProperty(“Id”,IsRequired=true)]
公共整数Id
{
获取{return(int)this[“Id”];}
设置{this[“Id”]=value;}
}
[配置属性(“列”,IsRequired=false)]
公共BounceProviderColumns
{
获取{return(BounceProviderColumns)this[“Columns”];}
设置{this[“Columns”]=value;}
}
公共静态BounceProviderConfig GetByProviderId(int providerId)
{
var section=ConfigUtils.GetConfigurationSection(“BounceProviders”);
foreach(第.Providers节中的BounceProviderConfig提供程序)
{
if(provider.Id==providerId)
退货供应商;
}
返回null;
}
}
公共类BounceProviderColumns:ConfigurationElement
{
[ConfigurationProperty(“电子邮件”,IsRequired=true)]
公共配置电子邮件
{
获取{return(ColumnConfig)this[“Email”];}
设置{this[“Email”]=value;}
}
[配置属性(“日期”,IsRequired=true)]
公共日期列配置日期
{
获取{return(DateColumnConfig)this[“Date”];}
设置{this[“Date”]=value;}
}
[配置属性(“消息”,IsRequired=true)]
公共列配置消息
{
获取{return(ColumnConfig)this[“Message”];}
设置{this[“Message”]=value;}
}        
}
公共类ColumnConfig:ConfigurationElement
{
[配置属性(“名称”,IsRequired=true)]
公共字符串名
{
获取{return(string)this[“Name”];}
设置{this[“Name”]=value;}
}
}

一点注意:
返回元素。GetHashCode()
:这是不正确的,哈希代码不是唯一的。您的代码可能会在此处出现异常。