EntLib 4.1 第一章 Configuration-Migration(
系统
2019-08-12 09:27:20
1547 0
一开始没接触过的先看看TerryLee的文章吧:
Enterprise Library Step By Step
系列(一):配置应用程序块——
入门篇
Enterprise Library Step By Step
系列(二):配置应用程序块——
进阶篇
以下讲解4.1与2.0的不同
首先我们来看自定义数据类
1
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来看
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>节点下添加你自定义的节点的信息如:
<
configSections
>
<
section
name
="EditorSettings"
type
="ConsoleApplication1.EditorFontData, ConsoleApplication1, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null"
/>
</
configSections
>
然后添加你自定义的节点
在工程中添加引用System.Configureation,如图所示:
读的代码:
EditorFontData configData
=
ConfigurationManager.GetSection(
"
EditorSettings
"
)
as
EditorFontData;
写的代码:
//
Write the new configuration data to the XML file
Configuration config
=
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.Sections.Remove(
"
EditorSettings
"
);
config.Sections.Add(
"
EditorSettings
"
, configData);
config.Save();
清除缓存的代码:
ConfigurationManager.RefreshSection(
"
EditorSettings
"
);
EntLib 4.1 第一章 Configuration-Migration(配置应用程序块)
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061
微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】 元
喜欢作者
Aspose.Word 微软Azure开始支持Docker技术