亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

ConfigurationSettings類解析

系統 2050 0

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 16.5pt; HEIGHT: 16.5pt" alt="" type="#_x0000_t75"><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image001.gif"></imagedata></shape>

.Net Framework 源碼分析

.Net Framework 的源碼是微軟編程大師們智慧的結晶,是我們開發人員夢寐以求的知識寶藏。

挖掘這座寶藏是我們快速提升自身編程思想水平的重要途徑。

下面是我研究分析 .Net Framework 一部分代碼后的一點心得,共享出來,希望對大家有所幫助,當然,分析不對的地方,還望指正,不勝感激。

System.Configuration.ConfigurationSettings

相信大家對這個類都不陌生吧。 ConfigurationSettings 類重要的方法是 ( 在我下面的分析中,方法也包括屬性 )

  • AppSettings 屬性 用于獲取 元素配置節中的配置設置。

  • GetConfig 方法 返回用戶定義的配置節的配置設置。

在我們的項目開發中,我們經常通過 ConfigurationSettings.AppSettings["myKey"] 的方法 來獲取 web.config 配置項上 appSettings 的配置值。調用這個 ConfigurationSettings.AppSettings["myKey"] 索引器我們就可以獲取到 web.cofing 配置項 appSettings 的配置值,這太方便了。如果要我們設計一個這樣的功能的時候,我們會有什么想法呢。 我的想法大概的是這樣的:

1. 加載 web.config 配置文件的內容

2. 分析 web.config 配置文件配置項 appSettings 節點的內容,并加載到配置項管理類中。

3. 配置項管理類中應該有一個索引器,方便外部系統訪問。

讓我們來分析大師們是如何實現這個類的。看看大師級人物的代碼和設計思路有何高明之處。

//ConfigurationSettings 類的定義

public sealed class ConfigurationSettings

{

}

C# 關鍵字 sealed 表明此類是不能被繼承的。

// 靜態構造函數

static ConfigurationSettings()

{

_initState = InitState.NotStarted;

_initLock = new object();

}

一個類最先運行的代碼段就是靜態構造函數,并且對于整個程序域而言靜態構造函數只運行一次。
C#
關鍵字 static 加上類名稱的方法函數就是靜態構造函數。
對于一個類來說,只能有一個靜態構造函數。
靜態構造函數的作用主要是初始化靜態變量。
C# 關鍵字 static 約束的類方法里面的代碼都只能調用 靜態變量或者靜態方法 , 靜態屬性等。

靜態方法: C# 關鍵字 static 約束的方法就是靜態方法 ( 有些教材可能會稱為類方法 ) ,里面的代碼都只能調用 靜態變量或者靜態方法 , 靜態屬性等。

// 靜態變量的定義代碼

private static object _initLock;

C# 關鍵字 static 表明此變量為靜態變量。

// 構造函數

private ConfigurationSettings()

{

}

發現上面的構造函數跟我們平時所寫的類的構造函數有什么不同嗎?
對了,就是訪問權限的約束關鍵字 private  ,平時構造函數的約束關鍵字都是 public
那么將構造函數訪問權限設置為 private 有什么目的呢?

1. 防止別人的代碼通過 new 操作生成對象實例。

如: System.Configuration.ConfigurationSettings config = new System.Configuration.ConfigurationSettings();

你會發現上面的代碼編譯不通過,原因就是訪問了 private 的構造函數,當然編譯不通過啦!

2. 保證一個類僅有一個實例。

這里就是設計模式中的 Singleton 單件模式了,設置構造函數的訪問權限為 private 是實現 Singleton 模式的前提

//AppSettings 靜態只讀屬性

public static NameValueCollection AppSettings

{

get

{

ReadOnlyNameValueCollection config = (ReadOnlyNameValueCollection) GetConfig(" appSettings ");

if (config == null)

{

config = new ReadOnlyNameValueCollection(new

CaseInsensitiveHashCodeProvider(CultureInfo.InvariantCulture), new CaseInsensitiveComparer(CultureInfo.InvariantCulture));

config.SetReadOnly();

}

return config;

}

}

通過上面的代碼我們可以知道,此屬性為靜態只讀屬性 (static 關鍵字,只有 get 操作,而沒有 set 操作 )
因為 NameValueCollection 類定義了索引訪問器,所以平時我們的代碼都是這樣寫的 ConfigurationSettings.AppSettings["myKey"]
,對于 ["myKey"] 這種使用 [] 號訪問的索引器,我們下面分析 NameValueCollection 類時再說明索引器。

ReadOnlyNameValueCollection config = (ReadOnlyNameValueCollection) GetConfig(" appSettings ");
注意到參數的值是 appSettings 了嗎?
是不是跟我們 web.config 里面的 appSettings 的配置節點項有關聯呢?他們有什么關系嗎?我們往下看。
這段代碼調用了 ConfigurationSettings 類的另外一個靜態方法,代碼如下:

public static object GetConfig(string sectionName) // 當然這時 sectionName == "appSettings"

{

if ((sectionName == null) || (sectionName.Length == 0))

// 判斷 string 的值是不是為 Empty 時,應該用 sectionName.Length == 0  來判斷

{

return null;

}

if (_initState < InitState.Usable)

{

EnsureConfigurationSystem();

}

if (_initError != null)

{

throw _initError;

}

return _configSystem.GetConfig(sectionName);

}



代碼段:

if (_initState < InitState.Usable)

{

EnsureConfigurationSystem();

}

InitState 只是一個私有的枚舉類型 enum

private enum InitState

{

NotStarted,

Started,

Usable,

Completed

}

剛才 ConfigurationSettings 類的靜態構造函數是設置
initState = InitState.NotStarted;
那么第一次運行時 , 肯定會執行 EnsureConfigurationSystem() 方法了 , 我們接著看看代碼的實現

private static void EnsureConfigurationSystem()

{

lock (_initLock)

{

if (_initState < InitState.Usable)

{

_initState = InitState.Started;

try

{

_configSystem = new DefaultConfigurationSystem();

_initState = InitState.Usable;

}

catch (Exception exception)

{

_initError = exception;

_initState = InitState.Completed;

throw;

}

}

}

}

C# 關鍵字 lock 加鎖處理。
lock 確保當一個線程位于代碼的臨界區時,另一個線程不進入臨界區。如果其他線程試圖進入一個鎖定代碼,則它將在釋放該對象前一直等待(塊)。
MSDN 的解釋: lock 關鍵字可將語句塊標記為臨界區,方法是獲取給定對象的互斥鎖,執行語句,然后釋放該鎖。
通常,如果要保護實例變量,則 lock(this) ;如果要保護 static 變量(或者如果臨界區出現在給定類的靜態方法中),則 lock(typeOf (class))

<shape id="_x0000_i1026" style="WIDTH: 9.75pt; HEIGHT: 11.25pt" alt="" type="#_x0000_t75"><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image002.gif"></imagedata></shape>2007-7-24 13:57:32

陳英豪

<shape id="_x0000_i1027" style="WIDTH: 24pt; HEIGHT: 24pt" alt="" type="#_x0000_t75"><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image003.gif"></imagedata></shape>
<shape id="_x0000_i1028" style="WIDTH: 82.5pt; HEIGHT: 7.5pt" alt="" type="#_x0000_t75"><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image004.gif"></imagedata></shape>
等級:版主
文章: 17
積分: 19
注冊: 2007-7-2

<shape id="_x0000_i1029" style="WIDTH: 45pt; HEIGHT: 13.5pt" alt="給2AE6E5308869449895CF7EDAAB814568發送一個短消息" type="#_x0000_t75" o:button="t" target="_blank" ><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image005.gif"></imagedata></shape> <shape id="_x0000_i1030" style="WIDTH: 36pt; HEIGHT: 13.5pt" alt="把2AE6E5308869449895CF7EDAAB814568加入好友" type="#_x0000_t75" o:button="t" target="_blank" ><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image006.gif"></imagedata></shape> <shape id="_x0000_i1031" style="WIDTH: 33.75pt; HEIGHT: 13.5pt" alt="查看2AE6E5308869449895CF7EDAAB814568的個人資料" type="#_x0000_t75" o:button="t" target="_blank" ><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image007.gif"></imagedata></shape> <shape id="_x0000_i1032" style="WIDTH: 33.75pt; HEIGHT: 13.5pt" alt="搜索2AE6E5308869449895CF7EDAAB814568的所有貼子" type="#_x0000_t75" o:button="t" target="_blank" ><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image008.gif"></imagedata></shape> <shape id="_x0000_i1033" style="WIDTH: 33.75pt; HEIGHT: 13.5pt" alt="回復這個貼子" type="#_x0000_t75" o:button="t" ><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image009.gif"></imagedata></shape>

2

<shape id="_x0000_i1034" style="WIDTH: 16.5pt; HEIGHT: 16.5pt" alt="" type="#_x0000_t75"><imagedata o: src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image001.gif"></imagedata></shape>

_configSystem = new DefaultConfigurationSystem();

private static IConfigurationSystem _configSystem;

_configSystem 是一個接口變量。
先看看接口 IConfigurationSystem 定義

public interface IConfigurationSystem

{

// Methods

object GetConfig(string configKey);

void Init();

}

接著我們跟蹤實現了 IConfigurationSystem 接口的 DefaultConfigurationSystem 類看看
類的定義:

internal class DefaultConfigurationSystem : IConfigurationSystem

{

}

C# 關鍵字 internal 表明此類只能被當前的 dll 里面的類使用。
順便提一提 protected internal 這樣的二個關鍵字的約束。它表明這個只能被當前 dll 里面的類使用或者不是當前 dll 里面的子類使用,記得是 或者 的關系
我們還是先從這個類的構造函數分析開始 :

internal DefaultConfigurationSystem()

{

}

這里的構造函數使用 internal ,并不是像 ConfigurationSettings 類構造函數的 private
它的訪問權限比 ConfigurationSettings 的類的松一點,允許當前 dll 里面的類可以通過 new 操作來生成多個 DefaultConfigurationSystem 實例。
所以這里才有上面的代碼 :
_configSystem = new DefaultConfigurationSystem();
的代碼調用。
重要方法 GetConfig 的部分關鍵代碼內容:

object IConfigurationSystem.GetConfig(string configKey) // 當然這里還是 configKey == "appSettings"

{

if (!this._isAppConfigInited)

{

this.EnsureInit(configKey);

}

ConfigurationRecord record = null;

if (record != null)

{

return record.GetConfig(configKey);

}

return null;

}

接下來我們就要分析 EnsureInit 方法。

private void EnsureInit(string configKey)

{

try

{

ConfigurationRecord record = new ConfigurationRecord();

bool flag2 = record.Load(this._machineFilename);

// 加載配置文件信息,這里是加載 machine.config 的信息,并不是 web.config 的信息

this._machineConfig = record;

///.... 省略

}

catch (Exception exception)

{

this._initError = exception;

background: #cccccc; margin:

分享到:
評論
wapysun
  • 瀏覽: 4877556 次
  • 性別: Icon_minigender_1
  • 來自: 杭州
最新評論

ConfigurationSettings類解析


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: jazzjazz国产精品久久 | 欧美成人精品欧美一级乱黄 | 女人精69xxxxx免费无毒 | 成年女人毛片免费视频 | 在线观看国产欧美 | 精品一成人岛国片在线观看 | 亚洲欧洲日本在线观看 | 亚洲欧美精品日韩欧美 | 国产高清一区二区三区视频 | 欧美激情伦妇在线观看 | 国产福利91 | 久久久精品日本一区二区三区 | 午夜久久久久久网站 | 草的爽免费视频 | 波多野结衣精品中文字幕 | 精品国产精品国产偷麻豆 | 91探花国产综合在线精品 | 二级毛片| 美国一级毛片aa | 波多野结衣亚洲 | 99视频在线观看视频 | 爱唯侦察1024入口地址 | 丁香婷婷成人 | 天天艹在线 | 日本乱中文字幕系列在线观看 | 国产精品久久久久久久久久影院 | 国产自产视频在线观看香蕉 | 鲁一鲁中文字幕久久 | 91视频一区二区 | 一级毛片无毒不卡直接观看 | 啊用力嗯快国产在线观看 | 99精品在线免费观看 | 亚洲综合首页 | 久久毛片免费看一区二区三区 | 婷婷综合五月中文字幕欧美 | 亚洲精品美女久久777777 | 在线观看免费精品国产 | 深夜福利免费 | 亚洲欧美国产五月天综合 | 久久精品一区二区三区不卡 | 午夜亚洲国产理论秋霞 |