4

我在 web.config 中有这个:

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

我想阅读所有部分“MySection”并获得所有价值List<string>(例如:“10”,“20”,“30”)

谢谢,

4

2 回答 2

5

首先,我推荐使用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>
于 2011-11-03T07:28:06.667 回答
2

我建议您查看 CodePlex 上优秀的开源配置部分设计器项目。它允许您使用 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);
}
于 2011-11-03T07:58:42.707 回答