ConfigSection 配置

来源:互联网 发布:java多久可以学会 编辑:程序博客网 时间:2024/05/22 08:03

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 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 床头灯什么颜色好 立式床头灯 宾馆床头灯 床头灯品牌排行榜 卧室壁灯床头灯 欧式卧室床头灯 陶瓷床头灯 床头灯 英语 床头灯怎么安装 床头灯英语读本系列 床头灯多高合适 虎牌床头柜保险柜 床头挂钟表好吗 一级床面40分钟 专业定制床 定制高低床 床床定制 定制一个普通床多少钱 定制榻榻米床多少钱 实木高低床定制 床定制 定制儿童高低床 隐形床定制 床大小 床标准高度 床的长度一般是多少 床的大小 2米的床长宽多少 床尺寸标准 学校床一般都多大尺寸 床1.5米规格尺寸 旅馆单人床尺寸 1米5的床尺寸 床尾凳尺寸 1.5米床被子尺寸 母猪双产床尺寸 学校单人床尺寸是多少 榻榻米床的尺寸 按摩床的尺寸 1.8米床被子尺寸 学校的床是什么尺寸