zoukankan      html  css  js  c++  java
  • c#中的自定义配置节

    技术人写博客,真是应该,这个自定义配置节很早以前就做了,但现在项目中又要用到了,以前的错误还是会犯,今天还是把这些都记下来吧。c#中的自定义配置节主要是由以下几个类来实现的:ConfigurationSection,ConfigurationElementCollection和ConfigurationElement,层次也很清楚。ConfigurationSection就是最上导的节点。放在

    <configSections>

        <section name="" type="" />

     </configSections>

    里,其中name就是节点的名称,type里就是以“程序所在完整路径,程序集名”。

    ConfigurationElementCollection和ConfigurationElement就是管下面真正的配置了。多话不说了,贴上我的测试代码吧!

    namespace Test
    {
        class TestSection : ConfigurationSection
        {
            [ConfigurationProperty("columns", IsRequired = true)]
            public ColumnElements Columns
            {
                get
                {
                    return this["columns"] as ColumnElements;
                }
    
                private set{}
            }
    
            [ConfigurationProperty("times", IsRequired = true)]
            public string Times
            {
                get
                {
                    return this["times"] as string;
                }
    
                private set { }
            }
        }
    }
    

      

    namespace Test
    {
        public class ColumnElements : ConfigurationElementCollection
        {
            //public ColumnElements()
            //{
            //    return new ColumnElement();
            //}
    
            #region 基类方法重载
    
            protected override ConfigurationElement CreateNewElement()
            {
                return new ColumnElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return (element as ColumnElement).Name;
            }
    
            public new ColumnElement this[string name]
            {
                get
                {
                    return BaseGet(name) as ColumnElement;
                }
            }
    
            public new ColumnElement this[int index]
            {
                get
                {
                    return BaseGet(index) as ColumnElement;
                }
            }
    
            protected override string ElementName
            {
                get
                {
                    return "column";
                }
            }
    
            public override ConfigurationElementCollectionType CollectionType
            {
                get
                {
                    return ConfigurationElementCollectionType.BasicMap;
                }
            }
            #endregion
    
            #region 私有方法
            
    
    
            #endregion
        }
    }
    

      

    namespace Test
    {
        public class ColumnElement : ConfigurationElement
        {
            #region 字段和属性
    
            [ConfigurationProperty("name", IsRequired = true)]
            public string Name
            {
                get
                {
                    return this["name"] as string;
                }
    
                private set{}
            }
    
            [ConfigurationProperty("type", IsRequired = true)]
            public string Type
            {
                get
                {
                    return this["type"] as string;
                }
    
                private set{}
            }
    
            [ConfigurationProperty("length", IsRequired = false)]
            public int? Length
            {
                get
                {
                    return (int?)this["length"];
                }
    
                private set{}
            }
    
            #endregion       
        }
    }
    

      最后一定要提醒的是:ConfigurationElementCollection的子类里必须要实现CollectionType方法和ElementName属性啊,不然肯定会出错了,切记啊!

  • 相关阅读:
    Linux下vim中文乱码问题
    phoniex初始化
    [Android] 修改ImageView的图片颜色
    [osx] android studio下修改avd的hosts文件
    [osx] 设置crontab
    [osx] 查看端口被占用
    [Ubuntu] change mouse scrolling between standard and natural
    [Vuejs] 关于vue-router里面的subRoutes
    [nodejs] Error: unable to verify the first certificate
    [SublimeText] 安装包管理
  • 原文地址:https://www.cnblogs.com/tingchen/p/2155662.html
Copyright © 2011-2022 走看看