C#中使用自定义配置
先来看一个常见的配置文件模板:
<configSections> //配置节声明区域,包含配置节和命名空间声明
<section> //配置节声明
<sectionGroup/> //定义配置节组
</section> //配置节组中的配置节声明
</configSections>
<appSettings/> //预定义配置节
<Custom element for configuration section> //配置节设置区域
</configuration>
自定义配置可分为:自定义配置节和自定义配置节组。
1. 自定义配置节
要使用自定义配置需要修改两处地方,一是在<configSections/>中声明配置节,二是在<Custom element for configuration section>处设置配置节的具体配置。
声明配置节语法:
<section>:声明新配置节
name:自定义配置节的名称
type:自定义配置节的类型,主要包括System.Configuration.SingleTagSectionHandler、System.Configuration.DictionarySectionHandler、System.Configuration.NameValueSectionHandler。
SingleTagSectionHandler 配置节返回类型为 Systems.Collections.IDictionary
DictionarySectionHandler 配置节返回类型为 Systems.Collections.IDictionary
NameValueSectionHandler 配置节返回类型为 Systems.Collections.Specialized.NameValueCollection
下面是一个完整的自定义配置节示例代码:
<configuration>
<configSections>
<section name="UrlString" type="System.Configuration.SingleTagSectionHandler"/>
<section name="UrlString2" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<UrlString action="add" paramString="id=1"></UrlString>
<UrlString2>
<add key="add" value="id=1"/>
<add key="edit" value="id=2"/>
</UrlString2>
</configuration>
程序中读取配置代码如下:
{
//访问 UrlString 配置节
IDictionary dict = ConfigurationManager.GetSection("UrlString") as IDictionary;
Console.WriteLine(string.Format("{0}?{1}", dict["action"], dict["paramString"]));
//访问 UrlString2 配置节
IDictionary dict2 = ConfigurationManager.GetSection("UrlString2") as IDictionary;
foreach (DictionaryEntry e in dict2)
{
Console.WriteLine(string.Format("{0}?{1}", e.Key, e.Value));
}
Console.Read();
}
2. 自定义配置节组
配置节组是使用<sectionGroup>元素来声明,在配置节组中可以包括多个配置节<section>,如下:
<configuration>
<configSections>
<sectionGroup name="TestGroup">
<section name="Test" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<TestGroup>
<Test>
<add key="Hello" value="World"/>
</Test>
</TestGroup>
</configuration>
下面是访问这个配置节组的代码:
MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]); //输出Hello World
3. 通过 IConfigurationSectionHandler 接口实现自定义配置处理类
IConfigurationSectionHandler 接口原型:
{
Object Create(Object parent, Object configContext, XmlNode section)
}
Create 方法必须可由多个线程同时调用,即要保证线程安全。
示例代码:
{
private string _action;
public string Action
{
get { return _action; }
set { _action = value; }
}
private string _param;
public string Param
{
get { return _param; }
set { _param = value; }
}
#region IConfigurationSectionHandler 成员
public object Create(object parent, object configContext, XmlNode section)
{
Hashtable hashtable = new Hashtable();
foreach (XmlAttribute attribute in section.Attributes)
{
hashtable[attribute.Name] = attribute.Value;
}
return hashtable;
}
#endregion
public static UrlString Create()
{
UrlString us = new UrlString();
Hashtable ht = ConfigurationManager.GetSection("UrlString") as Hashtable;
us.Action = (string)ht["action"];
us.Param = (string)ht["paramString"];
return us;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现