码迷,mamicode.com
首页 > 移动开发 > 详细

App.config自定义节点读取

时间:2018-07-23 11:04:12      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:.config   source   getc   inf   ram   cep   sha   pre   encoding   

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!--<otherconfig configSource="XmlConfig\other.config" />-->
  <otherconfig   a="1111" b="2222" c="333"/>
</configuration>
public class OtherConfigInfo : ConfigurationSection
    {
        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <returns></returns>
        public static OtherConfigInfo GetConfig()
        {
            return GetConfig("otherconfig");
        }
        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="sectionName">xml节点名称</param>
        /// <returns></returns>
        public static OtherConfigInfo GetConfig(string sectionName)
        {
            OtherConfigInfo section = (OtherConfigInfo)ConfigurationManager.GetSection(sectionName);
            if (section == null)
                throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
            return section;
        }

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

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

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

  

 //调用方法
OtherConfigInfo configInfo = OtherConfigInfo.GetConfig();
 Console.WriteLine(configInfo.a);

 

App.config自定义节点读取

标签:.config   source   getc   inf   ram   cep   sha   pre   encoding   

原文地址:https://www.cnblogs.com/xxxin/p/9352884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!