一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - 解決在Web.config或App.config中添加自定義配置的方法詳解

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

2019-11-07 12:06asp.net教程網 ASP.NET教程

本篇文章是對在Web.config或App.config中添加自定義配置的方法進行了詳細的分析介紹,需要的朋友參考下

.Net中的System.Configuration命名空間為我們在web.config或者app.config中自定義配置提供了完美的支持。最近看到一些項目中還在自定義xml文件做程序的配置,所以忍不住寫一篇用系統自定義配置的隨筆了。
如果你已經對自定義配置了如指掌,請忽略這篇文章。
言歸正傳,我們先來看一個最簡單的自定義配置

復制代碼代碼如下:


<?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中添加一個section元素,并制定此section元素對應的類型和名字。然后再在configuration根節點下面添加此自定義配置,如上例中的simple節點。simple節點只有兩個整形數的屬性maxValue和minValue。
要在程序中使用自定義配置我們還需要實現存取這個配置塊的類型,一般需要做如下三件事:
1. 定義類型從System.Configuration.ConfigurationSection繼承
2. 定義配置類的屬性,這些屬性需要用ConfigurationProperty特性修飾,并制定屬性在配置節中的名稱和其他一些限制信息
3. 通過基類的string索引器實現屬性的get ,set

非常簡單和自然,如下是上面配置類的實現:

復制代碼代碼如下:


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; 
        } 
    } 
}


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

復制代碼代碼如下:


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


這個配置類太過簡陋了,可能有時候我們還需要更復雜的構造,比如在配置類中使用類表示一組數據,下面我們看一個稍微復雜一點的自定義配置

復制代碼代碼如下:


<?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> 


這個配置的名字是complex,他有一個屬性height,他的節點內還有一個child元素這個元素有兩個屬性firstName和lastName;對于這個內嵌的節點該如何實現呢?首先我們需要定義一個類,要從ConfigurationElement類繼承,然后再用和SimpleSection類似的方法定義一些用ConfigurationProperty特性修飾的屬性就可以了,當然屬性值的get,set也要使用基類的索引器。如下實現:

復制代碼代碼如下:


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; 
        } 
    } 
}


還有稍微再復雜一點的情況,我們可能要在配置中配置一組相同類型的節點,也就是一組節點的集合。如下面的配置:

復制代碼代碼如下:


<?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> 


請看children節點,它就是一個集合類,在它里面定義了一組add元素,也可以有remove節點把已經添進去的配置去掉。
要使用自定義節點集合需要從ConfigurationElementCollection類繼承一個自定義類,然后要實現此類GetElementKey(ConfigurationElement element)和ConfigurationElement CreateNewElement()兩個方法;為了方便的訪問子節點可以在這個類里面定義只讀的索引器。請看下面的實現

復制代碼代碼如下:


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); 
        } 
    } 

}


當然要使用此集合類我們必須在Complex類中添加一個此集合類的屬性,并要指定集合類的元素類型等屬性,如下:

復制代碼代碼如下:


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


我們會經常用到類似appSettings配置節的鍵值對的構造,這時候我們就不必再自己實現了,我們可以直接使用現有的System.Configuration.NameValueConfigurationCollection類來定義一個自定義的鍵值對。可以在Complex類中定義如下屬性

復制代碼代碼如下:


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


然后在配置文件的complex節中添加鍵值對配置

復制代碼代碼如下:


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


到這兒已經基本上可以滿足所有的配置需求了。不過還有一點更大但是不復雜的概念,就是sectionGroup。我們可以自定義SectionGroup,然后在sectionGroup中配置多個section;分組對于大的應用程序是很有意義的。
如下配置,配置了一個包含simple和一個complex兩個section的sectionGroup

復制代碼代碼如下:


<?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我們可以實現一個繼承自System.Configuration.ConfigurationSectionGroup類的自定義類。實現很簡單,就是通過基類的Sections[“sectionName”]索引器返回Section。如下:

復制代碼代碼如下:


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]索引器獲得,如下示例代碼:

復制代碼代碼如下:


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


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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品国产免费观看一区高清 | 国产久草在线 | 亚洲精品视频在线免费 | 無码一区中文字幕少妇熟女网站 | 日韩免费在线看 | 91麻豆国产精品91久久久 | 亚洲爆操| 97久久精品午夜一区二区 | 国产精品久久久久久久人人看 | 美女视频91 | 韩国最新理论片奇忧影院 | 91久久青青草原线免费 | 亚洲国产成人精品不卡青青草原 | 亚洲AV久久无码精品蜜桃 | 我与旗袍老师疯狂床震 | 精品欧美 | 精品亚洲欧美中文字幕在线看 | 亲爱的客栈第二季免费观看完整版 | 成人国产在线视频在线观看 | 国产日韩在线 | 欧美日韩在线一区 | 美女的让男人桶爽免费看 | 91久久色 | 91在线视频国产 | 久久视频在线视频观看精品15 | www日本视频| 热国产热综合 | 亚洲成人一区 | 韩国三级 720p| 四虎影院2022 | 日本天堂视频在线观看 | 日本xxx片免费高清在线 | 亚洲小视频 | 欧美成人aa久久狼窝动画 | 欧亚尺码专线欧洲s码wmy | 无码国产成人午夜在线观看不卡 | 久久国产综合精品欧美 | 欧美精品色精品一区二区三区 | 欧美一级xxxx俄罗斯一级 | 猛h辣h高h文湿校园1v1 | 国产精品欧美在线观看 |