日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

ConfigurationSettings類(lèi)解析

系統(tǒng) 2438 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 的源碼是微軟編程大師們智慧的結(jié)晶,是我們開(kāi)發(fā)人員夢(mèng)寐以求的知識(shí)寶藏。

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

下面是我研究分析 .Net Framework 一部分代碼后的一點(diǎn)心得,共享出來(lái),希望對(duì)大家有所幫助,當(dāng)然,分析不對(duì)的地方,還望指正,不勝感激。

System.Configuration.ConfigurationSettings 類(lèi)

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

  • AppSettings 屬性 用于獲取 元素配置節(jié)中的配置設(shè)置。

  • GetConfig 方法 返回用戶(hù)定義的配置節(jié)的配置設(shè)置。

在我們的項(xiàng)目開(kāi)發(fā)中,我們經(jīng)常通過(guò) ConfigurationSettings.AppSettings["myKey"] 的方法 來(lái)獲取 web.config 配置項(xiàng)上 appSettings 的配置值。調(diào)用這個(gè) ConfigurationSettings.AppSettings["myKey"] 索引器我們就可以獲取到 web.cofing 配置項(xiàng) appSettings 的配置值,這太方便了。如果要我們?cè)O(shè)計(jì)一個(gè)這樣的功能的時(shí)候,我們會(huì)有什么想法呢。 我的想法大概的是這樣的:

1. 加載 web.config 配置文件的內(nèi)容

2. 分析 web.config 配置文件配置項(xiàng) appSettings 節(jié)點(diǎn)的內(nèi)容,并加載到配置項(xiàng)管理類(lèi)中。

3. 配置項(xiàng)管理類(lèi)中應(yīng)該有一個(gè)索引器,方便外部系統(tǒng)訪(fǎng)問(wèn)。

讓我們來(lái)分析大師們是如何實(shí)現(xiàn)這個(gè)類(lèi)的。看看大師級(jí)人物的代碼和設(shè)計(jì)思路有何高明之處。

//ConfigurationSettings 類(lèi)的定義

public sealed class ConfigurationSettings

{

}

C# 關(guān)鍵字 sealed 表明此類(lèi)是不能被繼承的。

// 靜態(tài)構(gòu)造函數(shù)

static ConfigurationSettings()

{

_initState = InitState.NotStarted;

_initLock = new object();

}

一個(gè)類(lèi)最先運(yùn)行的代碼段就是靜態(tài)構(gòu)造函數(shù),并且對(duì)于整個(gè)程序域而言靜態(tài)構(gòu)造函數(shù)只運(yùn)行一次。
C#
關(guān)鍵字 static 加上類(lèi)名稱(chēng)的方法函數(shù)就是靜態(tài)構(gòu)造函數(shù)。
對(duì)于一個(gè)類(lèi)來(lái)說(shuō),只能有一個(gè)靜態(tài)構(gòu)造函數(shù)。
靜態(tài)構(gòu)造函數(shù)的作用主要是初始化靜態(tài)變量。
C# 關(guān)鍵字 static 約束的類(lèi)方法里面的代碼都只能調(diào)用 靜態(tài)變量或者靜態(tài)方法 , 靜態(tài)屬性等。

靜態(tài)方法: C# 關(guān)鍵字 static 約束的方法就是靜態(tài)方法 ( 有些教材可能會(huì)稱(chēng)為類(lèi)方法 ) ,里面的代碼都只能調(diào)用 靜態(tài)變量或者靜態(tài)方法 , 靜態(tài)屬性等。

// 靜態(tài)變量的定義代碼

private static object _initLock;

C# 關(guān)鍵字 static 表明此變量為靜態(tài)變量。

// 構(gòu)造函數(shù)

private ConfigurationSettings()

{

}

發(fā)現(xiàn)上面的構(gòu)造函數(shù)跟我們平時(shí)所寫(xiě)的類(lèi)的構(gòu)造函數(shù)有什么不同嗎?
對(duì)了,就是訪(fǎng)問(wèn)權(quán)限的約束關(guān)鍵字 private  ,平時(shí)構(gòu)造函數(shù)的約束關(guān)鍵字都是 public
那么將構(gòu)造函數(shù)訪(fǎng)問(wèn)權(quán)限設(shè)置為 private 有什么目的呢?

1. 防止別人的代碼通過(guò) new 操作生成對(duì)象實(shí)例。

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

你會(huì)發(fā)現(xiàn)上面的代碼編譯不通過(guò),原因就是訪(fǎng)問(wèn)了 private 的構(gòu)造函數(shù),當(dāng)然編譯不通過(guò)啦!

2. 保證一個(gè)類(lèi)僅有一個(gè)實(shí)例。

這里就是設(shè)計(jì)模式中的 Singleton 單件模式了,設(shè)置構(gòu)造函數(shù)的訪(fǎng)問(wèn)權(quán)限為 private 是實(shí)現(xiàn) Singleton 模式的前提

//AppSettings 靜態(tài)只讀屬性

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;

}

}

通過(guò)上面的代碼我們可以知道,此屬性為靜態(tài)只讀屬性 (static 關(guān)鍵字,只有 get 操作,而沒(méi)有 set 操作 )
因?yàn)? NameValueCollection 類(lèi)定義了索引訪(fǎng)問(wèn)器,所以平時(shí)我們的代碼都是這樣寫(xiě)的 ConfigurationSettings.AppSettings["myKey"]
,對(duì)于 ["myKey"] 這種使用 [] 號(hào)訪(fǎng)問(wèn)的索引器,我們下面分析 NameValueCollection 類(lèi)時(shí)再說(shuō)明索引器。

ReadOnlyNameValueCollection config = (ReadOnlyNameValueCollection) GetConfig(" appSettings ");
注意到參數(shù)的值是 appSettings 了嗎?
是不是跟我們 web.config 里面的 appSettings 的配置節(jié)點(diǎn)項(xiàng)有關(guān)聯(lián)呢?他們有什么關(guān)系嗎?我們往下看。
這段代碼調(diào)用了 ConfigurationSettings 類(lèi)的另外一個(gè)靜態(tài)方法,代碼如下:

public static object GetConfig(string sectionName) // 當(dāng)然這時(shí) sectionName == "appSettings"

{

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

// 判斷 string 的值是不是為 Empty 時(shí),應(yīng)該用 sectionName.Length == 0  來(lái)判斷

{

return null;

}

if (_initState < InitState.Usable)

{

EnsureConfigurationSystem();

}

if (_initError != null)

{

throw _initError;

}

return _configSystem.GetConfig(sectionName);

}



代碼段:

if (_initState < InitState.Usable)

{

EnsureConfigurationSystem();

}

InitState 只是一個(gè)私有的枚舉類(lèi)型 enum

private enum InitState

{

NotStarted,

Started,

Usable,

Completed

}

剛才 ConfigurationSettings 類(lèi)的靜態(tài)構(gòu)造函數(shù)是設(shè)置
initState = InitState.NotStarted;
那么第一次運(yùn)行時(shí) , 肯定會(huì)執(zhí)行 EnsureConfigurationSystem() 方法了 , 我們接著看看代碼的實(shí)現(xiàn)

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# 關(guān)鍵字 lock 加鎖處理。
lock 確保當(dāng)一個(gè)線(xiàn)程位于代碼的臨界區(qū)時(shí),另一個(gè)線(xiàn)程不進(jìn)入臨界區(qū)。如果其他線(xiàn)程試圖進(jìn)入一個(gè)鎖定代碼,則它將在釋放該對(duì)象前一直等待(塊)。
MSDN 的解釋?zhuān)? lock 關(guān)鍵字可將語(yǔ)句塊標(biāo)記為臨界區(qū),方法是獲取給定對(duì)象的互斥鎖,執(zhí)行語(yǔ)句,然后釋放該鎖。
通常,如果要保護(hù)實(shí)例變量,則 lock(this) ;如果要保護(hù) static 變量(或者如果臨界區(qū)出現(xiàn)在給定類(lèi)的靜態(tài)方法中),則 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>
等級(jí):版主
文章: 17
積分: 19
注冊(cè): 2007-7-2

<shape id="_x0000_i1029" style="WIDTH: 45pt; HEIGHT: 13.5pt" alt="給2AE6E5308869449895CF7EDAAB814568發(fā)送一個(gè)短消息" 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的個(gè)人資料" 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="回復(fù)這個(gè)貼子" 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 是一個(gè)接口變量。
先看看接口 IConfigurationSystem 定義

public interface IConfigurationSystem

{

// Methods

object GetConfig(string configKey);

void Init();

}

接著我們跟蹤實(shí)現(xiàn)了 IConfigurationSystem 接口的 DefaultConfigurationSystem 類(lèi)看看
類(lèi)的定義:

internal class DefaultConfigurationSystem : IConfigurationSystem

{

}

C# 關(guān)鍵字 internal 表明此類(lèi)只能被當(dāng)前的 dll 里面的類(lèi)使用。
順便提一提 protected internal 這樣的二個(gè)關(guān)鍵字的約束。它表明這個(gè)只能被當(dāng)前 dll 里面的類(lèi)使用或者不是當(dāng)前 dll 里面的子類(lèi)使用,記得是 或者 的關(guān)系
我們還是先從這個(gè)類(lèi)的構(gòu)造函數(shù)分析開(kāi)始 :

internal DefaultConfigurationSystem()

{

}

這里的構(gòu)造函數(shù)使用 internal ,并不是像 ConfigurationSettings 類(lèi)構(gòu)造函數(shù)的 private
它的訪(fǎng)問(wèn)權(quán)限比 ConfigurationSettings 的類(lèi)的松一點(diǎn),允許當(dāng)前 dll 里面的類(lèi)可以通過(guò) new 操作來(lái)生成多個(gè) DefaultConfigurationSystem 實(shí)例。
所以這里才有上面的代碼 :
_configSystem = new DefaultConfigurationSystem();
的代碼調(diào)用。
重要方法 GetConfig 的部分關(guān)鍵代碼內(nèi)容:

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

{

if (!this._isAppConfigInited)

{

this.EnsureInit(configKey);

}

ConfigurationRecord record = null;

if (record != null)

{

return record.GetConfig(configKey);

}

return null;

}

接下來(lái)我們就要分析 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:

分享到:
評(píng)論
wapysun
  • 瀏覽: 4877556 次
  • 性別: Icon_minigender_1
  • 來(lái)自: 杭州
存檔分類(lèi)
最新評(píng)論

ConfigurationSettings類(lèi)解析


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 壶关县| 金坛市| 丽水市| 岳阳市| 南皮县| 昌都县| 洪江市| 大方县| 大冶市| 锡林郭勒盟| 南宫市| 大足县| 台安县| 望都县| 景德镇市| 乳源| 秭归县| 乐都县| 财经| 九台市| 都匀市| 唐山市| 土默特左旗| 东安县| 嘉义县| SHOW| 彰化县| 大厂| 万宁市| 南川市| 太仓市| 大同县| 铜梁县| 方正县| 城步| 吉安市| 宝坻区| 赤水市| 岑溪市| 台安县| 神农架林区|