欧美日韩精品在线,国内精品久久久久久久久,一级毛片恃级毛片直播,清纯唯美亚洲综合欧美色

解決在Web.config或App.config中添加自定義配置的方法詳解_.Net教程

編輯Tag賺U幣

推薦:深入本機(jī)影像生成器(Ngen.exe)工具使用方法詳解
本篇文章是對(duì)本機(jī)影像生成器(Ngen.exe)工具使用方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

.Net中的System.Configuration命名空間為我們?cè)趙eb.config或者app.config中自定義配置提供了完美的支持。最近看到一些項(xiàng)目中還在自定義xml文件做程序的配置,所以忍不住寫一篇用系統(tǒng)自定義配置的隨筆了。
如果你已經(jīng)對(duì)自定義配置了如指掌,請(qǐng)忽略這篇文章。
言歸正傳,我們先來看一個(gè)最簡(jiǎn)單的自定義配置
復(fù)制代碼 代碼如下:m.dounai2.com

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="simple" type="ConfigExample.Configuration.SimpleSection,ConfigExample"/>
</configSections>
<simple maxValue="20" minValue="1"></simple>
</configuration>

在配置文件中使用自定義配置,需要在configSections中添加一個(gè)section元素,并制定此section元素對(duì)應(yīng)的類型和名字。然后再在configuration根節(jié)點(diǎn)下面添加此自定義配置,如上例中的simple節(jié)點(diǎn)。simple節(jié)點(diǎn)只有兩個(gè)整形數(shù)的屬性maxValue和minValue。
要在程序中使用自定義配置我們還需要實(shí)現(xiàn)存取這個(gè)配置塊的類型,一般需要做如下三件事:
1. 定義類型從System.Configuration.ConfigurationSection繼承
2. 定義配置類的屬性,這些屬性需要用ConfigurationProperty特性修飾,并制定屬性在配置節(jié)中的名稱和其他一些限制信息
3. 通過基類的string索引器實(shí)現(xiàn)屬性的get ,set
非常簡(jiǎn)單和自然,如下是上面配置類的實(shí)現(xiàn):
復(fù)制代碼 代碼如下:m.dounai2.com

public class SimpleSection:System.Configuration.ConfigurationSection
{
[ConfigurationProperty("maxValue",IsRequired=false,DefaultValue=Int32.MaxValue)]
public int MaxValue
{
get
{
return (int)base["maxValue"];
}
set
{
base["maxValue"] = value;
}
}

[ConfigurationProperty("minValue",IsRequired=false,DefaultValue=1)]
public int MinValue
{
get { return (int) base["minValue"];}
set { base["minValue"] = value; }
}


[ConfigurationProperty("enabled",IsRequired=false,DefaultValue=true)]
public bool Enable
{
get
{
return (bool)base["enabled"];
}
set
{
base["enabled"] = value;
}
}
}

這樣子一個(gè)簡(jiǎn)單的配置類就完成了,怎么在程序中使用這個(gè)配置呢?需要使用ConfigurationManager類(要引用System.configuration.dll這個(gè)dll只有在.Net2.0之后的版本中才有)的GetSection方法獲得配置就可以了。如下代碼:
復(fù)制代碼 代碼如下:m.dounai2.com

SimpleSection simple = ConfigurationManager.GetSection("simple") as SimpleSection;
Console.WriteLine("simple minValue={0} maxValue = {1}",simple.MinValue,simple.MaxValue);

這個(gè)配置類太過簡(jiǎn)陋了,可能有時(shí)候我們還需要更復(fù)雜的構(gòu)造,比如在配置類中使用類表示一組數(shù)據(jù),下面我們看一個(gè)稍微復(fù)雜一點(diǎn)的自定義配置
復(fù)制代碼 代碼如下:m.dounai2.com

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="complex" type="ConfigExample.Configuration.ComplexSection,ConfigExample"/>
</configSections>
<complex height="190">
<child firstName="James" lastName="Bond"/>
</complex>
</configuration>

這個(gè)配置的名字是complex,他有一個(gè)屬性height,他的節(jié)點(diǎn)內(nèi)還有一個(gè)child元素這個(gè)元素有兩個(gè)屬性firstName和lastName;對(duì)于這個(gè)內(nèi)嵌的節(jié)點(diǎn)該如何實(shí)現(xiàn)呢?首先我們需要定義一個(gè)類,要從ConfigurationElement類繼承,然后再用和SimpleSection類似的方法定義一些用ConfigurationProperty特性修飾的屬性就可以了,當(dāng)然屬性值的get,set也要使用基類的索引器。如下實(shí)現(xiàn):
復(fù)制代碼 代碼如下:m.dounai2.com

public class ComplexSection : ConfigurationSection
{
[ConfigurationProperty("height", IsRequired = true)]
public int Height
{
get
{
return (int)base["height"];
}
set
{
base["height"] = value;
}
}

[ConfigurationProperty("child", IsDefaultCollection = false)]
public ChildSection Child
{
get
{
return (ChildSection)base["child"];
}
set
{
base["child"] = value;
}
}
}

public class ChildSection : ConfigurationElement
{
[ConfigurationProperty("firstName", IsRequired = true, IsKey = true)]
public string FirstName
{
get
{
return (string)base["firstName"];
}
set
{
base["firstName"] = value;
}
}

[ConfigurationProperty("lastName", IsRequired = true)]
public string LastName
{
get
{
return (string)base["lastName"];
}
set
{
base["lastName"] = value;
}
}
}

還有稍微再復(fù)雜一點(diǎn)的情況,我們可能要在配置中配置一組相同類型的節(jié)點(diǎn),也就是一組節(jié)點(diǎn)的集合。如下面的配置:
復(fù)制代碼 代碼如下:m.dounai2.com

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="complex" type="ConfigExample.Configuration.ComplexSection,ConfigExample"/>
</configSections>
<complex height="190">
<child firstName="James" lastName="Bond"/>

<children>
<add firstName="Zhao" lastName="yukai"/>
<add firstName="Lee" lastName="yukai"/>
<remove firstName="Zhao"/>
</children>
</complex>
</configuration>

請(qǐng)看children節(jié)點(diǎn),它就是一個(gè)集合類,在它里面定義了一組add元素,也可以有remove節(jié)點(diǎn)把已經(jīng)添進(jìn)去的配置去掉。
要使用自定義節(jié)點(diǎn)集合需要從ConfigurationElementCollection類繼承一個(gè)自定義類,然后要實(shí)現(xiàn)此類GetElementKey(ConfigurationElement element)和ConfigurationElement CreateNewElement()兩個(gè)方法;為了方便的訪問子節(jié)點(diǎn)可以在這個(gè)類里面定義只讀的索引器。請(qǐng)看下面的實(shí)現(xiàn)
復(fù)制代碼 代碼如下:m.dounai2.com

public class Children : ConfigurationElementCollection
{
protected override object GetElementKey(ConfigurationElement element)
{
return ((ChildSection)element).FirstName;
}

protected override ConfigurationElement CreateNewElement()
{
return new ChildSection();
}

public ChildSection this[int i]
{
get
{
return (ChildSection)base.BaseGet(i);
}
}

public ChildSection this[string key]
{
get
{
return (ChildSection)base.BaseGet(key);
}
}

}

當(dāng)然要使用此集合類我們必須在Complex類中添加一個(gè)此集合類的屬性,并要指定集合類的元素類型等屬性,如下:
復(fù)制代碼 代碼如下:m.dounai2.com

[ConfigurationProperty("children", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(ChildSection), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap, RemoveItemName = "remove")]
public Children Children
{
get
{
return (Children)base["children"];
}
set
{
base["children"] = value;
}
}

我們會(huì)經(jīng)常用到類似appSettings配置節(jié)的鍵值對(duì)的構(gòu)造,這時(shí)候我們就不必再自己實(shí)現(xiàn)了,我們可以直接使用現(xiàn)有的System.Configuration.NameValueConfigurationCollection類來定義一個(gè)自定義的鍵值對(duì)。可以在Complex類中定義如下屬性
復(fù)制代碼 代碼如下:m.dounai2.com

[ConfigurationProperty("NVs", IsDefaultCollection = false)]
public System.Configuration.NameValueConfigurationCollection NVs
{
get
{
return (NameValueConfigurationCollection)base["NVs"];
}
set
{
base["NVs"] = value;
}
}

然后在配置文件的complex節(jié)中添加鍵值對(duì)配置
復(fù)制代碼 代碼如下:m.dounai2.com

<NVs>
<add name="abc" value="123"/>
<add name="abcd" value="12d3"/>
</NVs>

到這兒已經(jīng)基本上可以滿足所有的配置需求了。不過還有一點(diǎn)更大但是不復(fù)雜的概念,就是sectionGroup。我們可以自定義SectionGroup,然后在sectionGroup中配置多個(gè)section;分組對(duì)于大的應(yīng)用程序是很有意義的。
如下配置,配置了一個(gè)包含simple和一個(gè)complex兩個(gè)section的sectionGroup
復(fù)制代碼 代碼如下:m.dounai2.com

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup type="ConfigExample.Configuration.SampleSectionGroup,ConfigExample" name="sampleGroup">
<section type="ConfigExample.Configuration.SimpleSection,ConfigExample" allowDefinition="Everywhere" name="simple" />
<section type="ConfigExample.Configuration.ComplexSection,ConfigExample" allowDefinition="Everywhere" name="complex"/>
</sectionGroup>
</configSections>
<sampleGroup>
<simple maxValue="20" minValue="1">
</simple>

<complex height="190">
<child firstName="James" lastName="Bond"/>
<children>
<add firstName="Zhao" lastName="yukai"/>
<add firstName="Lee" lastName="yukai"/>
<remove firstName="Zhao"/>
</children>
<NVs>
<add name="abc" value="123"/>
<add name="abcd" value="12d3"/>
</NVs>
</complex>
</sampleGroup>
</configuration>

為了方便的存取sectionGroup中的section我們可以實(shí)現(xiàn)一個(gè)繼承自System.Configuration.ConfigurationSectionGroup類的自定義類。實(shí)現(xiàn)很簡(jiǎn)單,就是通過基類的Sections[“sectionName”]索引器返回Section。如下:
復(fù)制代碼 代碼如下:m.dounai2.com

public class SampleSectionGroup : System.Configuration.ConfigurationSectionGroup
{
public SimpleSection Simple
{
get
{
return (SimpleSection)base.Sections["simple"];
}
}

public ComplexSection Complex
{
get
{
return (ComplexSection)base.Sections["complex"];
}
}
}

需要注意的是SectionGroup不能使用ConfigurationManager.GetSection(string)方法來獲得,要獲得sectionGroup必須通過Configuration類的SectionGroups[string]索引器獲得,如下示例代碼:
復(fù)制代碼 代碼如下:m.dounai2.com

SampleSectionGroup sample = (SampleSectionGroup)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["sampleGroup"];

總結(jié):
.Net framework給我們提供了一套很方便的配置庫,我們只需要繼承對(duì)應(yīng)的類簡(jiǎn)單的配置一下就可以方便的使用在web.config或者app.config中配置的自定義節(jié)點(diǎn)了。

分享:基于.Net中的協(xié)變與逆變的深入分析
本篇文章是對(duì).Net中的協(xié)變與逆變進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

來源:模板無憂//所屬分類:.Net教程/更新時(shí)間:2013-05-19
相關(guān).Net教程