C#自定义ConfigSections节

在Net2.0以上的版本对自定义configSection是件相对容易的事情.
比如,我们要创建以下自定义的配置节ConfigSectionsLib
<configuration>
<configSections>
<section name="ConfigSectionsLib" type="ConfigSectionsLib.IPSection,ConfigSectionsLib"/>
</configSections>
<ConfigSectionsLib configSource="ConfigFiles\IPAllow.config" />
......
<configuration>

IPAllow.config文件结构如下:

<?xml version="1.0" encoding="utf-8" ?>
<ConfigSectionsLib>
<IPItemElement>
<IPItem Name="1" IP="192.168.1.1"></IPItem>
<IPItem Name="2" IP="192.168.1.2"></IPItem>
<IPItem Name="3" IP="192.168.1.3"></IPItem>
<IPItem Name="4" IP="192.168.1.4"></IPItem>
</IPItemElement>
</ConfigSectionsLib>

我们该如何开始呢,
首先应该建个项目(名称空间为ConfigSectionsLib,  DLL文件名:ConfigSectionsLib.dll)
创建类:IPSection 让它继承ConfigurationSection
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.ComponentModel;

namespace ConfigSectionsLib
{
public class IPSection : ConfigurationSection
{
/// <summary>
/// Gets the IPItemElement element.
/// </summary>
[ConfigurationProperty("IPItemElement")]
public IPItemElementCollection IPItems
{
get { return (IPItemElementCollection)this["IPItemElement"]; }
}


}
}
这代表我们要创建一个IPItems属性,这个属性是个IPItemElementCollection集合,它的定义在下面:

[ConfigurationCollection(typeof(IPItemElement), AddItemName = "IPItem", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class IPItemElementCollection : ConfigurationElementCollection
{
public IPItemElement this[int index]
{
get { return (IPItemElement)base.BaseGet(index); }
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
base.BaseAdd(index, value);
}
}

public new IPItemElement this[string name]
{
get { return (IPItemElement)base.BaseGet(name); }
}


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

protected override object GetElementKey(ConfigurationElement element)
{
return (element as IPItemElement).Name;
}

public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}

protected override string ElementName
{
get { return "IPItem"; }
}

}
如果想创建跟appSettings和connectionStrings一样的配置节,就是由<add/>子节组成的配置节,
你可以这样定义IPItemElementCollection
[ConfigurationCollection(typeof(IPItemElement),  CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
public class IPItemElementCollection : ConfigurationElementCollection
{
public IPItemElement this[int index]
{
get { return (IPItemElement)base.BaseGet(index); }
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
base.BaseAdd(index, value);
}
}

public new IPItemElement this[string name]
{
get { return (IPItemElement)base.BaseGet(name); }
}


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

protected override object GetElementKey(ConfigurationElement element)
{
return (element as IPItemElement).Name;
}
       // 将下面的注释
// public override ConfigurationElementCollectionType CollectionType
// {
//     get { return ConfigurationElementCollectionType.BasicMap; }
//  }

// protected override string ElementName
// {
//      get { return "IPItem"; }
//  }

}
这种形式的配置节将以以下的形式出现:
<ConfigSectionsLib>
<IPItemElement>
<Add Name="1" IP="192.168.1.1"></Add>
<Add Name="2" IP="192.168.1.2"></Add>
<Add Name="3" IP="192.168.1.3"></Add>
<Add Name="4" IP="192.168.1.4"></Add>
</IPItemElement>
</ConfigSectionsLib>

剩下的就是IPItemElement的定义:
public class IPItemElement : ConfigurationElement
{
[ConfigurationProperty("Name", DefaultValue = "")]
public string Name
{

get { return this["Name"] as string; }

}

[ConfigurationProperty("IP", DefaultValue = "")]
public string IP
{
get { return this["IP"] as string; }

}
}

这样就大功告成了,我们就可以写个类来封装对它的访问:
public class CustomIP
{
private string mName;

public string Name
{
get { return mName; }
set { mName = value; }
}
private string mIP;

public string IP
{
get { return mIP; }
set { mIP = value; }
}


}

public class SectionSetting
{
static IPSection ipSection = ConfigurationManager.GetSection("ConfigSectionsLib") as IPSection;

public static List<CustomIP> GetCustomIPList()
{
List<CustomIP> lst = new List<CustomIP>();
foreach (IPItemElement item in ipSection.IPItems)
{
CustomIP cip = new CustomIP();
cip.IP = item.IP;
cip.Name = item.Name;
lst.Add(cip);
}
//这边返回的就是所有子节的集合
return lst;
}

}

你可能感兴趣的:(config)