


.Net configuration file - unified node configuration management
对于.Net中的自定义节点,如果要单独处理,就要为这个节点添加一个指定的类,如果我们在一个应用程序中,定义了很多个自定义节点的话,还按照这样做,就会多出很多个处理类来,为了避免太多的类,我们将所有自定节点都指定一个自定义节点作为入口,然后只写一个处理类,以此为入口,读取其他节点。
例如,配置文件只定义一个入口节点:
<!--通用配置存储方法--> <traceFact type="ClassLib.ConfigManager,ClassLib"> <forum name="TraceFact.Net Community"> <root url="http:192.168.24.204"/> <replyCount>20</replyCount> <pageSize>30</pageSize> <offlineTime>20</offlineTime> </forum> <blog> <root url="http:asdakfj.com"/> <urlMappings> <rewriteRule> <request>~/(\d{4})/Default\.aspx</request> <sendTo>~BlogDetail.aspx?year=$1</sendTo> </rewriteRule> </urlMappings> </blog> <mailServer address="www.baidu.com" userName="lhc" password="234r32r"/> <greetingStrategy type="ClassLib.ChineseGreeting,ClassLib"/> </traceFact>
处理类:
/// <summary> /// Class GeneralConfigHandler /// </summary> /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 10:50:40</remarks> public class GeneralConfigHandler:IConfigurationSectionHandler { /// <summary> /// 创建配置节处理程序。 /// </summary> /// <param name="parent">父对象。</param> /// <param name="configContext">配置上下文对象。</param> /// <param name="section">节 XML 节点。</param> /// <returns>创建的节处理程序对象。</returns> /// <exception cref="System.NotImplementedException"></exception> /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 10:50:42</remarks> public object Create(object parent, object configContext, XmlNode section) { //获取节点type属性的值 Type t=Type.GetType(section.Attributes["type"].Value);//返回configManager //直接将section进行传递 object[] paramters={section}; //将要创建的类型实例 object obj = null; try { obj = Activator.CreateInstance(t, paramters);//使用有参数的构造函数 } catch (Exception) { return null; } return obj; } }
上面的类主要是为了实现创建具体配置节点管理类,在配置节点的管理类中,存有各个子节点类的引用。
/// <summary> /// GeneralConfigHandler通过反射动态创建的,它实际上只是作为一个容器 ///这个类仅仅是作为一个容器,包含对其下具体节点配置的引用,并通过traceFact根节点,获取traceFact其下的子节点,然后再创建用于映射具体的子节点的类型实例 /// </summary> /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:06:21</remarks> public class ConfigManager { private XmlNode section; private FroumConfiguration forumConfig; //对于其他节点的处理强类型访问类也定义为成员变量放在这里。。。。。。。 public FroumConfiguration froumConfig { get { return forumConfig; } } public ConfigManager(XmlNode section) { this.section = section; forumConfig = new FroumConfiguration(section.SelectSingleNode("forum")); } } /// <summary> /// 具体子节点配置forum结点:处理<forum name="TraceFact.Net Community"> /// </summary> /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:22:08</remarks> public class FroumConfiguration{ private XmlNode forumNode; //将forum结点传递进来 public FroumConfiguration(XmlNode section){ this.forumNode=section; } /// <summary> /// Gets the name. <forum name="TraceFact.Net Community"> /// </summary> /// <value>The name.</value> /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:58:00</remarks> public string Name{ get{ return forumNode.Attributes["name"].Value; } } /// <summary> /// Gets the root URL.:<root url="http:192.168.24.204"/> /// </summary> /// <value>The root URL.</value> /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:56:08</remarks> public string RootUrl{ get{ return forumNode.SelectSingleNode("root").Attributes["url"].Value; } } /// <summary> /// Gets the size of the page.<pageSize>30</pageSize> /// </summary> /// <value>The size of the page.</value> /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:57:18</remarks> public int PageSize{ get{ return int.Parse(forumNode.SelectSingleNode("pageSize").InnerText); } } /// <summary> /// Gets the reply count.<replyCount>20</replyCount> /// </summary> /// <value>The reply count.</value> /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:57:03</remarks> public int ReplyCount{ get{ return int.Parse(forumNode.SelectSingleNode("replyCount").InnerText); } } /// <summary> /// Gets the offline time.<offlineTime>20</offlineTime> /// </summary> /// <value>The offline time.</value> /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:57:35</remarks> public int OfflineTime { get { return int.Parse(forumNode.SelectSingleNode("offlineTime").InnerText); } } }
这样,我们使用的时候,每次都用这一个类来实现对节点的访问:
ConfigManager config = ConfigurationManager.GetSection("traceFact") as ConfigManager; //取值 ltrName.Text = config.froumConfig.Name; ltrOfflineTime.Text = config.froumConfig.OfflineTime.ToString(); ltrPageSize.Text = config.froumConfig.PageSize.ToString(); ltrReplyCount.Text = config.froumConfig.ReplyCount.ToString(); ltrRootUrl.Text = config.froumConfig.RootUrl.ToString();
以上就是.Net配置文件——统一节点配置管理的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



If you bought your laptop from a mobile operator, you most likely had the option to activate an eSIM and use your cellular network to connect your computer to the Internet. With eSIM, you don't need to insert another physical SIM card into your laptop because it's already built-in. It is very useful when your device cannot connect to the network. How to check if my Windows 11 device is eSIM compatible? Click the Start button and go to Network & Internet > Cellular > Settings. If you don't see the "Cellular" option, your device doesn't have eSIM capabilities and you should check another option, such as using your mobile device to connect your laptop to a hotspot. In order to activate and

Why should we write the fixed file of the configuration file? We can directly write it as a .py file, such as settings.py or config.py. The advantage of this is that we can directly import parts of it through import in the same project; but if we need to use it in other When sharing configuration files on non-Python platforms, writing a single .py is not a good choice. At this time we should choose a common configuration file type to store these fixed parts. Currently, the commonly used and popular configuration file format types mainly include ini, json, toml, yaml, xml, etc. We can access these types of configuration files through standard libraries or third-party libraries.

Hyperf is an excellent PHP framework. Its main features are fast, flexible and scalable. It is currently widely used in the industry. In the process of developing using the Hyperf framework, we often encounter situations that require configuration management. This article will introduce how to use the Hyperf framework for configuration management and provide specific code examples. 1. The location of the configuration file. When developing using the Hyperf framework, the configuration file is usually placed in the config directory, or it can be entered in the .env file.

Setting up a wireless network is common, but choosing or changing the network type can be confusing, especially if you don't know the consequences. If you're looking for advice on how to change the network type from public to private or vice versa in Windows 11, read on for some helpful information. What are the different network profiles in Windows 11? Windows 11 comes with a number of network profiles, which are essentially sets of settings that can be used to configure various network connections. This is useful if you have multiple connections at home or office so you don't have to set it all up every time you connect to a new network. Private and public network profiles are two common types in Windows 11, but generally

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

Recently, many Win10 system users want to change the user profile, but they don’t know how to do it. This article will show you how to set the user profile in Win10 system! How to set up user profile in Win10 1. First, press the "Win+I" keys to open the settings interface, and click to enter the "System" settings. 2. Then, in the opened interface, click "About" on the left, then find and click "Advanced System Settings". 3. Then, in the pop-up window, switch to the "" option bar and click "User Configuration" below.

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

Helm is an important component of Kubernetes that simplifies the deployment of Kubernetes applications by bundling configuration files into a package called HelmChart. This approach makes updating a single configuration file more convenient than modifying multiple files. With Helm, users can easily deploy Kubernetes applications, simplifying the entire deployment process and improving efficiency. In this guide, I'll cover different ways to implement Helm on Ubuntu. Please note: The commands in the following guide apply to Ubuntu 22.04 as well as all Ubuntu versions and Debian-based distributions. These commands are tested and should work correctly on your system. in U
