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;
}
}