zoukankan      html  css  js  c++  java
  • 简单实现自定义配置节1

           在程序中经常要使用自定义配置节来存储一些信息,下面是一个只读简单实现:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;

    namespace SmsWinForm
    {

        
    public class SectionHandler : System.Configuration.IConfigurationSectionHandler
        {
            
    public object Create(object parent, object configContext, System.Xml.XmlNode section)
            {
                
    return new Config(section);
            }
        }

        
    class Config
        {
            
    private System.Xml.XmlNode m_section;

            
    public Config(System.Xml.XmlNode node)
            {
                m_section 
    = node;
            }

            
    static private Config configSection
            {
                
    get
                {
                    Config config 
    = (Config)System.Configuration.ConfigurationManager.GetSection("SmsWinForm");
                    
    if (config == null)
                    {
                        
    throw new ApplicationException("Failed to get configuration from App.config.");
                    }
                    
    return config;
                }
            }

            
    public string this[string key]
            {
                
    get
                {
                    System.Xml.XmlNode node 
    = m_section.SelectSingleNode(key);
                    
    if (node != null)
                        
    return node.InnerText;
                    
    else
                        
    return null;
                }
            }

            
    static public string BoardID
            {
                
    get
                {
                    
    return configSection["boardid"];
                }
            }

            
    static public string CategoryID
            {
                
    get
                {
                    
    return configSection["categoryid"];
                }
            }

        }
    }

    App.config中
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      
    <configSections>
        
    <section name="SmsWinForm" type="SmsWinForm.SectionHandler,ConsoleTest1"/>
      
    </configSections>
      
    <SmsWinForm>
        
    <categoryid>CMPP</categoryid>
        
    <boardid>32</boardid>
      
    </SmsWinForm>
    </configuration>

    调用


    Tip:如果只是用于只读情况下,那这个方法足够了,并且大部分情况我们很少更新配置节。

    http://wintersun.cnblogs.com

  • 相关阅读:
    《Fluent Python》 CH.06_函数_使用一等函数实现设计模式 (函数式策略模式+命令模式)
    《Python+Spark 2.0+Hadoop》第10章 Python Spark RDD 读书笔记 (转换/动作运算、广播变量、累加器、示例略)
    《Fluent Python》 CH.05_数据结构-一等函数 (函数对象的各种内置函数、operator模块、itemgetter 和 attrgetter迭代/取值函数)
    JAVA时间字符串去空格、冒号和横杠
    Maven 构建生命周期(compile与install的区别)
    MySQL-进程占用CPU资源高问题分析
    K8S-二进制安装部署高可用集群环境
    Oracle-DG环境中Gap
    MySQL-主从复制环境升级小版本软件
    SequoiaDB
  • 原文地址:https://www.cnblogs.com/wintersun/p/1339430.html
Copyright © 2011-2022 走看看