如何實作ConfigurationElementCollection
在上一篇文章,介紹如何實作ConfigurationElement來存取某一個設定檔內多個欄位值,
而本篇則介紹如何透過ConfigurationElementCollection來存取以下的設定集合:
<HackSettings>
<add OrderName="Kim"/>
<add OrderName="Jia"/>
</HackSettings>
1.實作ConfigurationElement

ConfigurationElement
001 
using ...System;
002
using System.Collections.Generic;
003
using System.Text;
004
using System.Configuration;
005
006
namespace Hack.Configuration
007 
...{
008
/// <summary>
009
///
010
/// </summary>
011
public class HackSettingElement : ConfigurationElement
012 
...{
013
014
// Test flag.
015
private static bool _displayIt = false;
016
017
public HackSettingElement(String newOrderName)
018 
...{
019
OrderName = newOrderName;
020
021
022
}
023
024
public HackSettingElement()
025 
...{
026
027
}
028
029
030
[ConfigurationProperty("OrderName",
031
IsRequired = true,
032
IsKey = true)]
033
public string OrderName
034 
...{
035
get
036 
...{
037
return (string)this["OrderName"];
038
}
039
set
040 
...{
041
this["OrderName"] = value;
042
}
043
}
044
045
046
protected override void DeserializeElement(
047
System.Xml.XmlReader reader,
048
bool serializeCollectionKey)
049 
...{
050
base.DeserializeElement(reader,
051
serializeCollectionKey);
052
053
// Enter your custom processing code here.
054
if (_displayIt)
055 
...{
056
Console.WriteLine(
057
"UrlConfigElement.DeserializeElement(...{0}, {1}) called",
058
(reader == null) ? "null" : reader.ToString(),
059
serializeCollectionKey.ToString());
060
}
061
}
062
063
064
protected override bool SerializeElement(
065
System.Xml.XmlWriter writer,
066
bool serializeCollectionKey)
067 
...{
068
bool ret = base.SerializeElement(writer,
069
serializeCollectionKey);
070
071
// Enter your custom processing code here.
072
073
if (_displayIt)
074 
...{
075
Console.WriteLine(
076
"UrlConfigElement.SerializeElement(...{0}, {1}) called = {2}",
077
(writer == null) ? "null" : writer.ToString(),
078
serializeCollectionKey.ToString(), ret.ToString());
079
}
080
return ret;
081
082
}
083
084
085
protected override bool IsModified()
086 
...{
087
bool ret = base.IsModified();
088
089
// Enter your custom processing code here.
090
091
Console.WriteLine("UrlConfigElement.IsModified() called.");
092
093
return ret;
094
}
095
096
097
}
098
099
100
}
2.實作ConfigurationElementCollection

ConfigurationElementCollection
001 
using ...System;
002
using System.Collections.Generic;
003
using System.Text;
004
using System.Configuration;
005
006
namespace Hack.Configuration
007 
...{
008
public class HackSettingCollection :
009
ConfigurationElementCollection
010 
...{
011
018 
覆寫不需更改#region 覆寫不需更改
019
public override
020
ConfigurationElementCollectionType CollectionType
021 
...{
022
get
023 
...{
024
return
025
ConfigurationElementCollectionType.AddRemoveClearMap;
026
}
027
}
028
029
#endregion
030
031 
覆寫更改型別#region 覆寫更改型別
032
protected override
033
ConfigurationElement CreateNewElement()
034 
...{
035
return new HackSettingElement();
036
}
037
protected override
038
ConfigurationElement CreateNewElement( string elementName)
039 
...{
040
return new HackSettingElement(elementName);
041
}
042
protected override Object
043
GetElementKey(ConfigurationElement element)
044 
...{
045
return ((HackSettingElement)element).OrderName;
046
}
047
#endregion
048
049
050
051
public new string AddElementName
052 
...{
053
get
054
...{ return base.AddElementName; }
055
056
set
057
...{ base.AddElementName = value; }
058
059
}
060
061
public new string ClearElementName
062 
...{
063
get
064
...{ return base.ClearElementName; }
065
066
set
067
...{ base.AddElementName = value; }
068
069
}
070
071
public new string RemoveElementName
072 
...{
073
get
074
...{ return base.RemoveElementName; }
075
076
077
}
078
079
public new int Count
080 
...{
081
082
get ...{ return base.Count; }
083
084
}
085
086
087
public HackSettingElement this[int index]
088 
...{
089
get
090 
...{
091
return (HackSettingElement)BaseGet(index);
092
}
093
set
094 
...{
095
if (BaseGet(index) != null)
096 
...{
097
BaseRemoveAt(index);
098
}
099
BaseAdd(index, value);
100
}
101
}
102
103
new public HackSettingElement this[string Name]
104 
...{
105
get
106 
...{
107
return (HackSettingElement)BaseGet(Name);
108
}
109
}
110
111
public int IndexOf(HackSettingElement url)
112 
...{
113
return BaseIndexOf(url);
114
}
115
116
public void Add(HackSettingElement url)
117 
...{
118
BaseAdd(url);
119
120
// Add custom code here.
121
}
122
123
protected override void
124
BaseAdd(ConfigurationElement element)
125 
...{
126
BaseAdd(element, false);
127
// Add custom code here.
128
}
129
130
public void Remove(HackSettingElement url)
131 
...{
132
if (BaseIndexOf(url) >= 0)
133
BaseRemove(url.OrderName);
134
}
135
136
public void RemoveAt(int index)
137 
...{
138
BaseRemoveAt(index);
139
}
140
141
public void Remove(string name)
142 
...{
143
BaseRemove(name);
144
}
145
146
public void Clear()
147 
...{
148
BaseClear();
149
// Add custom code here.
150
}
151
}
152
153
}
3.實作ConfigurationSection

ConfigurationSection
01 
using ...System;
02
using System.Collections.Generic;
03
using System.Text;
04
using System.Configuration;
05
06
namespace Hack.Configuration
07 
...{
08
/// <summary>
09
///
10
/// </summary>
11
public class HackConfigSection : ConfigurationSection
12 
...{
13
[ConfigurationProperty("HackSettings")]
14
public HackSettingCollection HackSettings
15 
...{
16
get
17 
...{
18
return (HackSettingCollection)base["HackSettings"];
19
}
20
21
}
22
23
}
24
25
26
}
4.組態設定
<configSections>
<section name="HackConfigSection" type="Kim.Configuration.HackConfigSection,Kim.Configuration"/>
</configSections>
<HackConfigSection>
<HackSettings>
<add OrderName="Kim"/>
<add OrderName="Jia"/>
</HackSettings>
</HackConfigSection>
Ps:紅字為命名空間+類別名稱
藍字為組件名稱
5.程式測試
KimSection kimObj = System.Configuration.ConfigurationManager.GetSection("Kim") as KimSection;
Response.Write("Username:" + kimObj.KimReportServer.Username + "<br>");
Response.Write("Password:" + kimObj.KimReportServer.Password + "<br>");
Response.Write("Url:" + kimObj.KimReportServer.Url + "<br>");
參考文件
http://msdn.microsoft.com/zh-tw/library/system.configuration.configurationsection.aspx
完整範例下載
ConfigurationSample.rar