ConfigSection 配置

Section配置为什么出现

用于完整的一个可插入套件配置,因为 AppSettings 给人的感觉有点零散,什么都可以往里面堆

Section的例子

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="work" type="BackgroundWorker.Configuration.WorkSection, BackgroundWorker" />
     </configSections>
     <work>
        <tasks>
            <task name="StartupReactor">
                <details>
                    <add name="time" value="2014-01-01T00:00:00" />
                    <add name="powerOutput" value="50%" />
                </details>
            </task>
            <task name="ProduceElectricity" />
            <task name="ShutdownReactor">
                <details>
                    <add name="time" value="2014-12-31T23:59:59" />
                </details>
            </task>
        </tasks>
    </work>
</configuration>

读取这个Section需要做哪些工作

添加以下类

1.WorkSection

2.TaskElementCollection

3.TaskElment

4.DetailElementCollection

5.DetailElement

//具体调用方式
var work = (WorkSection)ConfigurationManager.GetSection("work");
foreach (var task in work.Tasks.Cast<TaskElement>())
{
    Console.Out.WriteLine(task.Name);
    foreach (var detail in task.Details.Cast<DetailElement>())
    {
        Console.Out.WriteLine(string.Format("{0}={1}", detail.Name, detail.Value));
    }
}

WorkSection.cs 具体的代码

public class WorkSection : ConfigurationSection
{
    private static ConfigurationProperty tasks;
    private static ConfigurationPropertyCollection properties;
    static WorkSection()
    {
        tasks = new ConfigurationProperty("tasks", typeof(TaskElementCollection), null, ConfigurationPropertyOptions.IsRequired);
         properties = new ConfigurationPropertyCollection
        {
            tasks
        };
    }
    public TaskElementCollection Tasks
    {
         get
        {
            return (TaskElementCollection)this[tasks];
        }
        set
        {
            this[tasks] = value;
        }
    }
    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return properties;
        }
    }
}

TaskElement.cs 具体的代码

public class TaskElement : ConfigurationElement
{
    private static ConfigurationProperty name;

    private static ConfigurationProperty details;

    private static ConfigurationPropertyCollection properties;

    static TaskElement()
    {
        name = new ConfigurationProperty("name", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);
        details = new ConfigurationProperty("details", typeof(DetailElementCollection));

        properties = new ConfigurationPropertyCollection
        {
            name,
            details
        };
    }

    public TaskElement()
        : base()
    {
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return properties;
        }
    }

    public string Name
    {
        get
        {
            return (string)this[name];
        }
        set
        {
            this[name] = value;
        }
    }

    public DetailElementCollection Details
    {
        get
        {
            return (DetailElementCollection)this[details];
        }
        set
        {
            this[details] = value;
        }
    }
}

DetailElement.cs 的具体代码

public class DetailElement : ConfigurationElement
{
    private static ConfigurationProperty name;

    private static ConfigurationProperty value;

    private static ConfigurationPropertyCollection properties;

    static DetailElement()
    {
        name = new ConfigurationProperty("name", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);
        value = new ConfigurationProperty("value", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);

        properties = new ConfigurationPropertyCollection
        {
            name,
            value
        };
    }

    public DetailElement()
        : base()
    {
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return properties;
        }
    }

    public string Name
    {
        get
        {
            return (string)this[name];
        }
        set
        {
            this[name] = value;
        }
    }

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

DetailElementCollection.cs 的具体代码

public class DetailElementCollection : ConfigurationElementCollection
{
    public DetailElementCollection()
        : base()
    {
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            // set the style of our collection;
            // "AddRemoveClearMap" in this case
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        var detailElement = (DetailElement)element;

        return detailElement.Name;
    }
}

TaskElementCollection.cs 的具体代码

public class TaskElementCollection : ConfigurationElementCollection
{
    public TaskElementCollection()
        : base()
    {
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            // set the style of our collection;
            // "BasicMap" in this case
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    // set the name of the sub element;
    // "task" in this case
    protected override string ElementName
    {
        get
        {
            return "task";
        }
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        var taskElement = (TaskElement)element;

        return taskElement.Name;
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值