Home >Backend Development >C#.Net Tutorial >.Net - implement the IConfigurationSectionHandler interface to define handlers to process custom nodes
In addition to using the built-in handler provided in .net to process our custom nodes, we can also use a variety of methods to define our own processing classes to process our custom nodes. This article mainly introduces the implementation IConfigurationSectionHandler interface to implement custom handlers.
First, we first write the following custom node in the configuration file:
<!-- 使用自定义节点和上面的自定义处理程序--> <mailServerGroup provider="www.baidu.com"> <mailServer client="http://blog.csdn.net/lhc1105"> <address>13232@qq.com</address> <userName>lhc</userName> <password>2343254</password> </mailServer> <mailServer client="http://www.cnblogs.com/liuhuichao/"> <address>132345232@qq.com</address> <userName>水田如雅</userName> <password>2343453254</password> </mailServer> </mailServerGroup>
Then write the corresponding class for processing:
namespace MailHandlerClass { public class MailServer { //存储mailServer的子节点(<address>13232@qq.com</address><userName>lhc</userName><password>2343254</password>)的值 //以及client的值 private Hashtable serverNode; //构造函数 public MailServer() { serverNode = new Hashtable(); } public Hashtable ServerNode { get { return serverNode; } } public string client { get { return serverNode["client"] as string; } } public string Address { get { return serverNode["address"] as string; } } public string UserName { get { return serverNode["userName"] as string; } } public string PassWord { get { return serverNode["password"] as string; } } } //对应mailServerGroup public class MailServerConfig : List<MailServer> { //映射provider值 public string Provider { get; set; } } //自定义配置节点mailServerGroup的处理程序 public class MailServerConfigurationHandler : IConfigurationSectionHandler { //section为MailServerGroup节点 public object Create(object parent, object configContext, System.Xml.XmlNode section) { //设置方法返回配置对象,可以是任何类型 MailServerConfig config = new MailServerConfig(); //获取节点的属性信息 config.Provider = section.Attributes["provider"] == null ? "" : section.Attributes["provider"].Value; //获取MailServer节点 foreach (System.Xml.XmlNode child in section.ChildNodes) { MailServer server = new MailServer(); //添加Client属性 if (child.Attributes["client"]!=null) { server.ServerNode.Add("client", child.Attributes["client"].Value); } //获取MailServer下的Name,username,password节点 foreach (System.Xml.XmlNode grandChild in child.ChildNodes) { //添加文本 server.ServerNode.Add(grandChild.Name, grandChild.InnerText); } //将server加入MailServerConfig config.Add(server); } return config; } } }
In fact, it can be seen from the code that the implementation idea of the custom processing class is still to use hashtable to realize key-value reading and storage + XML processing;
After that, we associate the above classes and nodes in the configuration file:
<configuration> <!--定义处理mailServerGroup配置节的类--> <configSections> <section name="mailServerGroup" type="MailHandlerClass.MailServerConfigurationHandler,实现IConfigurationSectionHandler接口"/> <!--【注意】type:指定用来执行如下操作的配置节处理程序类的名称:处理在 name 属性中指定的节或元素中的配置设置。使用以下格式:type=" Fully qualified class name , assembly file name , version , culture , public key token "定义必须匹配程序集引用。--> </configSections>
Be sure to pay attention to the type parameter when specifying the value s position! ! ! ! ! Otherwise, there will be an error that the handler cannot be loaded.
Write a piece of code to test:
namespace MailHandlerClass { //也可以通过继承ConfigurationSection类来完成 class Program { static void Main(string[] args) { MailServerConfig mailServerConfig = (MailServerConfig)ConfigurationManager.GetSection("mailServerGroup"); //读取节点值 mailServerConfig.ForEach(m => { Console.WriteLine(m.client+";"+m.Address+";"+m.UserName+";"+m.PassWord); }); } } }
Well, in fact, it is recommended to force the conversion to as or something.
In fact, the configuration file looks like this at first:
After that, when you look at the configuration file, it will look like this: # The above is .Net - implement the IConfigurationSectionHandler interface to define the handler to process the content of the custom node. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!