Enterprise Library Step By Step 系列(一):配置应用程序块—— 入门篇
Enterprise Library Step By Step 系列(二):配置应用程序块—— 进阶篇
以下讲解4.1与2.0的不同
首先我们来看自定义数据类
using
System.Text;
2
using
System.Configuration;
3
4
namespace
ConfigurationMigrationQuickStart
5
{
6
/**/
///
<summary>
7
///
Summary description for ConfigurationData.
8
///
</summary>
9
public
class
EditorFontData : ConfigurationSection
10
{
11
12
public
EditorFontData()
13
{
14
}
15
16
[ConfigurationProperty(
"
name
"
)]
17
public
string
Name
18
{
19
get
{
return
(
string
)
this
[
"
name
"
]; }
20
set
{
this
[
"
name
"
]
=
value; }
21
}
22
23
[ConfigurationProperty(
"
size
"
)]
24
public
float
Size
25
{
26
get
{
return
(
float
)
this
[
"
size
"
]; }
27
set
{
this
[
"
size
"
]
=
value; }
28
}
29
30
[ConfigurationProperty(
"
style
"
)]
31
public
int
Style
32
{
33
get
{
return
(
int
)
this
[
"
style
"
]; }
34
set
{
this
[
"
style
"
]
=
value; }
35
}
36
37
public
override
string
ToString()
38
{
39
StringBuilder sb
=
new
StringBuilder();
40
sb.AppendFormat(
"
Name = {0}; Size = {1}; Style = {2}
"
, Name, Size.ToString(), Style.ToString());
41
42
return
sb.ToString();
43
}
44
}
45
}
46
用属性(Attribution)来标记了对应的属性(Property)
如[ConfigurationProperty(
"
name
"
)]
下面结合App.config来看
1
<?
xml version="1.0" encoding="utf-8"
?>
2
<
configuration
>
3
<
configSections
>
4
<
section
name
="EditorSettings"
type
="ConsoleApplication1.EditorFontData, ConsoleApplication1, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null"
/>
5
</
configSections
>
6
<
EditorSettings
name
="Verdana"
size
="24"
style
="2"
/>
7
</
configuration
>
可见,每个属性(Attribute)都对应着XML节点的属性
与2.0不同的是,4.1中好像没法用用Enterprise Library Configuration配置应用程序
以下是配置的详细步骤:
先建立一个项目,
然后建立自定义配置数据类,
在App.config或Web.config中添加节点<configSections>
在<configSections>节点下添加你自定义的节点的信息如:
< section name ="EditorSettings" type ="ConsoleApplication1.EditorFontData, ConsoleApplication1, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null" />
</ configSections >
然后添加你自定义的节点
在工程中添加引用System.Configureation,如图所示:
读的代码:
写的代码:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.Sections.Remove( " EditorSettings " );
config.Sections.Add( " EditorSettings " , configData);
config.Save();
清除缓存的代码:

