EntLib 4.1 第一章 Configuration-Migration(配置应用程序块)
一开始没接触过的先看看TerryLee的文章吧:
Enterprise Library Step By Step系列(一):配置应用程序块——入门篇
Enterprise Library Step By Step系列(二):配置应用程序块——进阶篇
以下讲解4.1与2.0的不同
首先我们来看自定义数据类
App.config
Enterprise Library Step By Step系列(一):配置应用程序块——入门篇
Enterprise Library Step By Step系列(二):配置应用程序块——进阶篇
以下讲解4.1与2.0的不同
首先我们来看自定义数据类
1
using System.Text;
2
using System.Configuration;
3![](/Images/OutliningIndicators/None.gif)
4
namespace ConfigurationMigrationQuickStart
5
{
6
/// <summary>
7
/// Summary description for ConfigurationData.
8
/// </summary>
9
public class EditorFontData : ConfigurationSection
10
{
11![](/Images/OutliningIndicators/InBlock.gif)
12
public EditorFontData()
13
{
14
}
15![](/Images/OutliningIndicators/InBlock.gif)
16
[ConfigurationProperty("name")]
17
public string Name
18
{
19
get { return (string)this["name"]; }
20
set{ this["name"] = value; }
21
}
22![](/Images/OutliningIndicators/InBlock.gif)
23
[ConfigurationProperty("size")]
24
public float Size
25
{
26
get{ return (float)this["size"]; }
27
set{ this["size"] = value; }
28
}
29![](/Images/OutliningIndicators/InBlock.gif)
30
[ConfigurationProperty("style")]
31
public int Style
32
{
33
get { return (int)this["style"]; }
34
set{ this["style"] = value; }
35
}
36![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
42
return sb.ToString();
43
}
44
}
45
}
46![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
46
![](/Images/OutliningIndicators/None.gif)
用属性(Attribution)来标记了对应的属性(Property)
如[ConfigurationProperty("name")]
下面结合App.config来看
![](/Images/OutliningIndicators/ContractedBlock.gif)
可见,每个属性(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,如图所示:
![Ref.JPG](/images/cnblogs_com/hcoona/Ref.JPG)
读的代码:
写的代码:
清除缓存的代码:
<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();
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.Sections.Remove("EditorSettings");
config.Sections.Add("EditorSettings", configData);
config.Save();
清除缓存的代码:
ConfigurationManager.RefreshSection("EditorSettings");