欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

ConfigurationSettings類解析

系統(tǒng) 2135 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é)晶,是我們開發(fā)人員夢寐以求的知識寶藏。

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

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

System.Configuration.ConfigurationSettings

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

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

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

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

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

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

3. 配置項管理類中應(yīng)該有一個索引器,方便外部系統(tǒng)訪問。

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

//ConfigurationSettings 類的定義

public sealed class ConfigurationSettings

{

}

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

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

static ConfigurationSettings()

{

_initState = InitState.NotStarted;

_initLock = new object();

}

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

靜態(tài)方法: C# 關(guān)鍵字 static 約束的方法就是靜態(tà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ù)跟我們平時所寫的類的構(gòu)造函數(shù)有什么不同嗎?
對了,就是訪問權(quán)限的約束關(guān)鍵字 private  ,平時構(gòu)造函數(shù)的約束關(guān)鍵字都是 public
那么將構(gòu)造函數(shù)訪問權(quán)限設(shè)置為 private 有什么目的呢?

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

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

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

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

這里就是設(shè)計模式中的 Singleton 單件模式了,設(shè)置構(gòu)造函數(shù)的訪問權(quán)限為 private 是實現(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;

}

}

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

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

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

{

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

// 判斷 string 的值是不是為 Empty 時,應(yīng)該用 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 類的靜態(tài)構(gòu)造函數(shù)是設(shè)置
initState = InitState.NotStarted;
那么第一次運行時 , 肯定會執(zhí)行 EnsureConfigurationSystem() 方法了 , 我們接著看看代碼的實現(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)一個線程位于代碼的臨界區(qū)時,另一個線程不進(jìn)入臨界區(qū)。如果其他線程試圖進(jìn)入一個鎖定代碼,則它將在釋放該對象前一直等待(塊)。
MSDN 的解釋: lock 關(guān)鍵字可將語句塊標(biāo)記為臨界區(qū),方法是獲取給定對象的互斥鎖,執(zhí)行語句,然后釋放該鎖。
通常,如果要保護(hù)實例變量,則 lock(this) ;如果要保護(hù) static 變量(或者如果臨界區(qū)出現(xiàn)在給定類的靜態(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>
等級:版主
文章: 17
積分: 19
注冊: 2007-7-2

<shape id="_x0000_i1029" style="WIDTH: 45pt; HEIGHT: 13.5pt" alt="給2AE6E5308869449895CF7EDAAB814568發(fā)送一個短消息" 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="回復(fù)這個貼子" 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();

}

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

internal class DefaultConfigurationSystem : IConfigurationSystem

{

}

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

internal DefaultConfigurationSystem()

{

}

這里的構(gòu)造函數(shù)使用 internal ,并不是像 ConfigurationSettings 類構(gòu)造函數(shù)的 private
它的訪問權(quán)限比 ConfigurationSettings 的類的松一點,允許當(dāng)前 dll 里面的類可以通過 new 操作來生成多個 DefaultConfigurationSystem 實例。
所以這里才有上面的代碼 :
_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;

}

接下來我們就要分析 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
  • 來自: 杭州
社區(qū)版塊
最新評論

ConfigurationSettings類解析


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久久久国产精品一区 | 国产精品久久久久无码人妻精品 | 天天色综合影视 | 91精品国产综合久久久密闭 | 精品一区二区三区免费毛片 | 成人国产精品齐天大性 | 国产精品美女 | 欧美大胆一级视频 | 午夜电影免费看 | 成人伊人| 热@国产| 山岸逢花在线观看 | 国产这里有精品 | 99久久精约久久久久久清纯 | 一级片视频网站 | 欧美日韩福利视频 | 91中文字幕在线 | 亚洲啊v在线观看 | 91精品一区二区三区久久久久久 | 精品久久洲久久久久护士 | 国产精品久久久久久影视 | 成人免费看片视频 | 毛片免费一区二区三区 | 国产精品毛片一区二区三区 | 99精品视频一区在线视频免费观看 | 小草激情视频 | 久久99精品视频 | 国产丫丫视频私人影院 | jizz国产精品免费麻豆 | 涩涩色综合亚洲悠悠色 | 龙珠z在线观看 | 国产高清精品一区二区三区 | 免费高清欧美一区二区视频 | 国产精品久久久久9999高清 | 成人免费福利 | 国产97色在线 | 日韩 | 亚洲日本中文字幕在线2022 | 欧美精品午夜久久久伊人 | 丁香六月激情婷婷 | 欧美精品免费xxxxx视频 | 国产成人精品高清免费 |