.net 自定义配置文件

两个部分实现,

分别实现 ConfigurationSectionGroup,ConfigurationSection

上面是配置文件配置项指定,

 <configSections>
    <sectionGroup name="ftyGroup" type="MxWeiXinPF.WS.common.Configurations.SectionGroup,MxWeiXinPF.WS"> 
      <section name="Sections" type="MxWeiXinPF.WS.common.Configurations.ServicesConfigurationSection,MxWeiXinPF.WS"/>
    </sectionGroup>  
  </configSections>

 

样例,复制一个SB的写法

  <!--微信公众平台-->
  <ftyGroup>
    <Sections>
      <WxConfigs> 
        <WxConfig MemberID="xx" WID="1" AppID="xx" AppSecret="xx" /> 
      </WxConfigs> 
      
      <Security userName="xx" pwd="xx" SymmetryKey="xx" />

      <Templates>
        <Template  WID="1" Key="t1" Value="-" />
        <Template  WID="1" Key="t2" Value="-" />
      </Templates>
      
    </Sections>
  </ftyGroup> 

 

实现的过程,先要有一个SectionGroup,来看

    using System.Configuration;

    public class SectionGroup : ConfigurationSectionGroup
    {
        public ServicesConfigurationSection FYTSections
        {
            get
            {
                return (base.Sections["Sections"] as ServicesConfigurationSection);
            }
        }
    }

然后实现一个ConfigurationSection,来,配置结点

    using System.Configuration;

    public class ServicesConfigurationSection : ConfigurationSection
    {
        /// <summary>
        /// 微信公众平台配置
        /// </summary>
        [ConfigurationProperty("WxConfigs", IsDefaultCollection = false)]
        public WxConfigElementCollection WxConfigs
        {
            get
            {
                return (WxConfigElementCollection)base["WxConfigs"];
            }


        }
这个是键值对的,其它的是List结构的
        /// <summary>
        /// 加密配置
        /// </summary>
        [ConfigurationProperty("Security", IsDefaultCollection = false)]
        public FytSecurityElement Security
        {
            get
            {
                return (FytSecurityElement)base["Security"];
            }


        }

        /// <summary>
        /// 微信公众号模板
        /// </summary>
        [ConfigurationProperty("Templates", IsDefaultCollection = false)]
        public TemplateElementCollection Templates
        {
            get
            {
                return (TemplateElementCollection)base["Templates"];
            }


        }
    }

然后是ConfigurationElementCollection,元素集合,这里对应的是上面的那个List结构,键值对不要这个,直接下面的结点元素就行了

    using System;
    using System.Configuration;
    using System.Reflection;

    /// <summary>
    /// 微信公众平台配置
    /// </summary>
    [ConfigurationCollection(typeof(WxConfigElement), AddItemName = "WxConfig")]
    public class WxConfigElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new WxConfigElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((WxConfigElement)element).MemberID;
        }

        public WxConfigElement this[int index]
        {
            get
            {
                return (WxConfigElement)base.BaseGet(index);
            }
        }

        public WxConfigElement this[string type]
        {
            get
            {
                return (WxConfigElement)base.BaseGet(type);
            }
        }
    }

 

最后是ConfigurationElement,元素,是吧

 using System;
    using System.Configuration;

    /// <summary>
    /// 微信公众平台配置
    /// </summary>
    public class WxConfigElement : ConfigurationElement
    { 
        [ConfigurationProperty("MemberID", IsRequired = false, IsKey=true)]
        public string MemberID
        {
            get
            {
                return (string)base["MemberID"];
            }
            set
            {
                base["MemberID"] = value;
            }
        }

        [ConfigurationProperty("WID", IsRequired = false, IsKey = true)]
        public string WID
        {
            get
            {
                return (string)base["WID"];
            }
            set
            {
                base["WID"] = value;
            }
        }

        [ConfigurationProperty("AppID", IsRequired = false)]
        public string AppID
        {
            get
            {
                return (string)base["AppID"];
            }
            set
            {
                base["AppID"] = value;
            }
        }

        [ConfigurationProperty("AppSecret", IsRequired = false)]
        public string AppSecret
        {
            get
            {
                return (string)base["AppSecret"];
            }
            set
            {
                base["AppSecret"] = value;
            }
        } 
    }

搞完了,跑一跑,上面有个点注意一下,这里的例子包含有键值对和数组两种实现

 

 

 

 

 

 

 

 

 

 

 

 

 

作者:zc
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2015-11-03 10:45  卖果子的小男孩  阅读(393)  评论(0编辑  收藏  举报