国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

如何使用.NET配置文件(二)

2024-07-10 12:56:07
字體:
來源:轉載
供稿:網友

    3. 自定義配置結構 (使用iconfigurationsectionhandler)
 假設有以下的配置信息,其在myinfo可以重復許多次,那么應如何讀取配置呢?這時就要使用自定義的配置程序了。
<myconfigs>
  <myinfo area="fuzhou" device="printer" customer="muf" />
  <myinfo area="shanghai" device="mobile" customer="liny" />
</myconfig>
 訪問代碼如下:
hashtable cfgtable = (hashtable)configurationsettings.getconfig( "myconfigs" );

debug.assert( cfgtable.count == 2);
hashtable cfgfuzhou = (hashtable)cfgtable["fuzhou"];
hashtable cfgshanghai = (hashtable)cfgtable["shanghai"];
debug.assert( cfgfuzhou["device"] == "printer" );
debug.assert( cfgshanghai["device"] == "mobile" );
debug.assert( cfgfuzhou["customer"] == "muf" );
debug.assert( cfgshanghai["customer"] == "liny" );

foreach(hashtable cfg in cfgtable.values)
{
 console.writeline("area={0} device={1} customer={2}", cfg["area"], cfg["device"], cfg["customer"]);
}

 為了能使用上面的訪問代碼來訪問配置結構,我們需要生成一個特定的配置讀取類(configurationsectionhandler),例子很簡單,就不多做說明了:
public class myinfosectionhandler: iconfigurationsectionhandler
{
 public object create(object parent, object configcontext, system.xml.xmlnode section)
 {
  hashtable config = new hashtable();
  foreach(xmlnode node in section.childnodes)
  {
   if(node.name != "myinfo")
    throw new system.configuration.configurationexception("不可識別的配置項", node);

   hashtable item = new hashtable();
   foreach(xmlattribute attr in node.attributes)
   {
    switch(attr.name)
    {
     case "area":
     case "device":
     case "customer":
      item.add(attr.name, attr.value);
      break;
     default:
      throw new system.configuration.configurationexception("不可識別的配置屬性", attr);
    }
   }
   config.add(item["area"], item);
  }
  return config;
 }
}

 然后,我們再定義配置說明。其中,mynamespace.myinfosectionhandler 是myinfosectionhandler類的帶名字空間的完整名稱;myapp 則是定義myinfosectionhandler類的程序集不帶擴展名的名字(如myapp.dll或myapp.exe):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- 以下是自定義配置的聲明 -->
  <configsections>
      <section name="myconfig" type="mynamespace.myinfosectionhandler, myapp" />
  </configsections>   
  <myconfigs>
    <myinfo area="fuzhou" device="printer" customer="muf" />
    <myinfo area="shanghai" device="mobile" customer="liny" />
  </myconfig>
</configuration>

 根據上面的例子,我們可以使用iconfigurationsectionhandler來實現任意的配置文件結構。
 
 (待續)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐昌市| 大悟县| 紫云| 炉霍县| 乐安县| 柳州市| 东宁县| 中宁县| 湟中县| 太康县| 两当县| 铅山县| 姜堰市| 黄石市| 邻水| 巴彦淖尔市| 嘉鱼县| 右玉县| 长治县| 闽清县| 清新县| 唐河县| 盱眙县| 斗六市| 成都市| 南昌市| 深州市| 杭州市| 江门市| 三原县| 宁强县| 克什克腾旗| 富源县| 沙湾县| 伊金霍洛旗| 屏边| 汪清县| 岳池县| 华宁县| 类乌齐县| 社会|