【问题标题】:Read web.config section to List阅读 web.config 部分以列出
【发布时间】:2011-11-03 19:48:12
【问题描述】:

我在 web.config 中有这个:

<MySection>
    <Setting1 Value="10" />
    <Setting2 Value="20" />
    <Setting3 Value="30" />
    <Setting4 Value="40" />
</MySection>

我想阅读“MySection”的所有部分,并将所有值设置为 List&lt;string&gt;(例如:“10”、“20”、“30”)

谢谢,

【问题讨论】:

标签: c# .net configuration configurationsection custom-configuration


【解决方案1】:

首先,我推荐使用Unity Configuration

代码:

public class MySection : ConfigurationSection
{
    protected static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();

    private static ConfigurationProperty propElements = new ConfigurationProperty("elements", typeof(MyElementCollection), null, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection);

    static BotSection()
    {
        properties.Add(propElements);
    }

    [ConfigurationProperty("elements", DefaultValue = null, IsRequired = true)]
    [ConfigurationCollection(typeof(MyElementCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
    public MyElementCollection Elements
    {
        get
        {
            return (MyElementCollection)this[propElements];
        }
        set
        {
            this[propElements] = value;
        }
    }
}

public class MyElementCollection : ConfigurationElementCollection, 
                                   IEnumerable<ConfigurationElement> // most important difference with default solution
{
    public void Add(MyElement element)
    {
        base.BaseAdd(element);
    }

    public void Clear()
    {
        base.BaseClear();
    }

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

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

    IEnumerator<MyElement> IEnumerable<MyElement>.GetEnumerator()
    {
        return this.OfType<MyElement>().GetEnumerator();
    }
}

public class MyElement : ConfigurationElement
{
    protected static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();

    private static ConfigurationProperty propValue= new ConfigurationProperty("value", typeof(int), -1, ConfigurationPropertyOptions.IsRequired);

    public int Value
    {
        get
        {
            return (int)this[propValue];
        }
        set
        {
            this[propValue] = value;
        }
    }
}

配置:

<configuration>
    <configSections>
        <section name="MySection" type="MySection, MyAssembly"/>
    </configSections>
    <MySection>
        <elements>
            <clear />
            <add value="10" />
            <remove value="10" />
            <add value="20" />
            <add value="30" />
        </elements>
    </MySection>
</configuration>

【讨论】:

  • 或者那行不通,或者缺少一块,或者我很愚蠢但那行不通
  • @Kris-I:我添加了更多代码,但您肯定需要阅读有关该主题的更多内容,自己尝试更多,然后发布一些结果或错误(如果有)
  • @Kris-I:看看这里。 MyElementCollection 已经包含元素,现在添加属性MyAttribute
【解决方案2】:

我建议您查看 CodePlex 上出色的开源 Configuration Section Designer 项目。它允许您使用 Visual Studio 中托管的设计器创建自定义配置部分。

例如,像这样的自定义配置部分设计:

将产生如下配置文件:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="MySection" type="MyNamespace.MySection, MyAssembly"/>
  </configSections>
  <MySection xmlns="urn:MyNamespace">
    <MySetting Name="Test1" Value="One" />
    <MySetting Name="Test2" Value="Two" />
  </MySection>
</configuration>

可以像这样以编程方式使用:

foreach (MySetting setting in MySection.Instance.Items)
{
    Console.WriteLine("{0}: {1}", setting.Name, setting.Value);
}

【讨论】:

  • @Kris-I:当然它还不存在。你需要先定义它。
猜你喜欢
  • 2011-12-30
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 2022-08-10
  • 2012-09-28
  • 1970-01-01
相关资源
最近更新 更多