zoukankan      html  css  js  c++  java
  • .NET 配置文件简单使用

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

    1.使用appSettings

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

    image

    程序中可以用key去读取:

    string objContainer = ConfigurationManager.AppSettings["objectContainer"];

    简单实用但是不够优雅。

    2.实现自己的配置节点

    image

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

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

    方法1:

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

    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特性来实现

     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

    这个方法我不太喜欢,它会自己生成配置文件对应的Class。不说了。
  • 相关阅读:
    原生Python机器学习分类之一Knn算法
    Java可视化文件(夹)加密解密压缩解压
    基于图搜索技术的八数码问题求解C++
    遗传算法解决TSP问题
    简单dp
    并查集
    KMP算法
    快速迭代
    为什么vs2017在代码右键上没有vs2013(第一个图)上实现抽象类这个选项?
    关于C#面向对象中的查看类图(没有此按键的问题)的解决方法 The solution to view class diagrams in C # object-oriented (without this key)
  • 原文地址:https://www.cnblogs.com/kklldog/p/3353018.html
Copyright © 2011-2022 走看看