前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.NET 配置文件简单使用

.NET 配置文件简单使用

作者头像
MJ.Zhou
发布2018-01-04 16:17:00
6210
发布2018-01-04 16:17:00
举报
文章被收录于专栏:.NET开发那点事.NET开发那点事

      当我们开发系统的时候要把一部分设置提取到外部的时候,那么就要用到.NET的配置文件了。比如我的框架中使用哪个IOC容器需要可以灵活的选择,那我就需要把IOC容器的设置提取到配置文件中去配置。实现有几种方法。

1.使用appSettings

这个是最简单的可以设置和读取的用户设置

image
image

程序中可以用key去读取:

代码语言:javascript
复制
string objContainer = ConfigurationManager.AppSettings["objectContainer"];

2.实现自己的配置节点

image
image

首先在configSections节点配置自己的配置解析类。

那么如何来解析这段配置呢?有两个办法。

方法1:

实现IConfigurationSectionHandler接口来自己解析配置文件的xml文件。

代码语言:javascript
复制
public class ObjectContainerElement
{
          public string Provider {get;set;}
          public string IocModule {get; set;}
}
public class AgileFRConfigurationHandler: IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
               
               var node =section.ChildNodes[0];
                if (node.Name != "objectContainer")
                    throw new ConfigurationErrorsException("不可识别的配置项", node);
            var config = new ObjectContainerElement();
                foreach (XmlAttribute attr in node.Attributes)
                {
                    switch (attr.Name)
                    {
                        case "provider":
                            config. Provider = attr.Value;
                            break;
                        case "iocModule":
                            config .IocModule = attr.Value;
                            break;
                        default:
                            throw new ConfigurationErrorsException("不可识别的配置属性", attr);
                    }
                }
            }
            return config;
        }
        //使用
   var config = ConfigurationManager.GetSection("agileFRConfiguration") as ObjectContainerElement;

这个方法看上去就略屌了,不过就是太麻烦了。

方法2:

继承ConfigurationSection类,配合ConfigurationProperty特性来实现

代码语言:javascript
复制
 public class ObjectContainerElement : ConfigurationElement
    {
        [ConfigurationProperty("provider", IsRequired = true)]
        public string Provider
        {
            get
            {
                return (string)this["provider"];
            }
            set
            {
                this["provider"] = (object)value;
            }
        }
        [ConfigurationProperty("iocModule", IsRequired = false)]
        public string IocModule
        {
            get
            {
                return (string)this["iocModule"];
            }
            set
            {
                this["iocModule"] = (object)value;
            }
        }
    }

 /// <summary>
    /// 配置处理类
    /// </summary>
    public class AgileFRConfigurationHandler : ConfigurationSection
    {
        [ConfigurationProperty("objectContainer", IsRequired = true)]
        public ObjectContainerElement ObjectContainer
        {
            get
            {
                return (ObjectContainerElement)this["objectContainer"];
            }
            set
            {
                this["objectContainer"] = (object)value;
            }
        }
    }//使用
    var configurationHandler = (AgileFRConfigurationHandler)ConfigurationManager.GetSection("agileFRConfiguration");
    var objectContainer=configurationHandler.ObjectContainer;
    这个方法简单优雅,我喜欢。

3.Settings.settings

代码语言:javascript
复制
这个方法我不太喜欢,它会自己生成配置文件对应的Class。不说了。
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-10-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.使用appSettings
  • 2.实现自己的配置节点
    • 方法1:
      • 方法2:
      • 3.Settings.settings
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档