增加 ConfigurationElementCollection的做法
讓組態的內容更具彈性
方案架構如同上個範例:組態檔 ConfigSection 的使用
此範例在 ConfigSection 內加上 ConfigurationElementCollection
如需多個 ConfigurationElement 不須逐一宣告,用 foreach 就可以逐一取出
ConfigurationDemo中的 App.config 內容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Demo1" type="ConfigurationDemo.LibData.DemoSection, LibData" />
<section name="Demo2" type="ConfigurationDemo.LibData.DemoSection, LibData" />
<section name="Demo3" type="ConfigurationDemo.LibData.DemoSection, LibData" />
</configSections>
<Demo1 ID="1" Name="本機" >
<domain url="localhost" />
<urls>
<url host="a.localhost" port="30080" name="本機A" />
<url host="b.localhost" port="30081" name="本機B" />
<url host="c.localhost" port="30082" name="本機C" />
</urls>
</Demo1>
<Demo2 ID="2" Name="測試機" >
<domain url="test.com" />
<urls>
<url host="a.test.com" port="8080" name="測試A" />
<url host="b.test.com" port="8081" name="測試B" />
<url host="c.test.com" port="8082" name="測試C" />
</urls>
</Demo2>
<Demo3 ID="3" Name="正式機" >
<domain url="demo.com" />
<urls>
<url host="a.demo.com" port="80" name="網址A" />
<url host="b.demo.com" port="80" name="網址B" />
<url host="c.demo.com" port="80" name="網址C" />
</urls>
</Demo3>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
LibData 內容為:
using System.Configuration;
namespace ConfigurationDemo.LibData
{
public class DemoSection : ConfigurationSection
{
[ConfigurationProperty("ID")]
public int ID
{
get { return (int)this["ID"]; }
}
[ConfigurationProperty("Name")]
public string Name
{
get { return (string)this["Name"]; }
}
[ConfigurationProperty("domain")]
public DomainElement Domain
{
get { return this["domain"] as DomainElement; }
}
[ConfigurationProperty("urls")]
[ConfigurationCollection(typeof(UrlElement), AddItemName = "url")]
public UrlsElementColection Urls
{
get { return this["urls"] as UrlsElementColection; }
}
}
public class UrlsElementColection : ConfigurationElementCollection
{
public UrlElement this[int index]
{
get { return (UrlElement)this.BaseGet(index); }
}
protected override ConfigurationElement CreateNewElement()
{
return new UrlElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as UrlElement).Host;
}
}
public class UrlElement : ConfigurationElement
{
[ConfigurationProperty("host")]
public string Host
{
get { return this["host"] as string; }
}
[ConfigurationProperty("port")]
public string Port
{
get { return this["port"] as string; }
}
[ConfigurationProperty("name")]
public string Name
{
get { return this["name"] as string; }
}
}
public class DomainElement : ConfigurationElement
{
[ConfigurationProperty("url")]
public string Url
{
get { return this["url"] as string; }
}
}
}
ConfigurationDemo > Program.cs 執行程式的語法 如下:
using ConfigurationDemo.LibData;
using System;
using System.Configuration;
namespace ConfigurationDemo
{
class Program
{
static void Main(string[] args)
{
DemoSection ds1 = ConfigurationManager.GetSection("Demo1") as DemoSection;
PrintSiteSection(ds1);
Console.WriteLine();
DemoSection ds2 = ConfigurationManager.GetSection("Demo2") as DemoSection;
PrintSiteSection(ds2);
Console.WriteLine();
DemoSection ds3 = ConfigurationManager.GetSection("Demo3") as DemoSection;
PrintSiteSection(ds3);
Console.ReadLine();
}
public static void PrintSiteSection(DemoSection section)
{
Console.WriteLine($"SectionName:{section.SectionInformation.Name}\tid:{section.ID}\tname:{section.Name}");
Console.WriteLine("└ElementName:" + nameof(section.Domain));
PrintDomainElement(section.Domain);
Console.WriteLine("└UrlsName:" + nameof(section.Urls));
PrintUrlsElementColection(section.Urls);
}
public static void PrintDomainElement(DomainElement element)
{
Console.WriteLine($" └─Url:{element.Url}");
}
public static void PrintUrlsElementColection(UrlsElementColection elementCollection)
{
foreach (UrlElement element in elementCollection)
{
PrintUrlElement(element);
}
}
public static void PrintUrlElement(UrlElement element)
{
Console.WriteLine($" └─Host:{element.Host}\tPort:{element.Port}\tName:{element.Name}");
}
}
}
執行結果: