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属性啊,不然肯定会出错了,切记啊!

  • 相关阅读:
    poj 2485 Highways 最小生成树
    hdu 3415 Max Sum of MaxKsubsequence
    poj 3026 Borg Maze
    poj 2823 Sliding Window 单调队列
    poj 1258 AgriNet
    hdu 1045 Fire Net (二分图匹配)
    poj 1789 Truck History MST(最小生成树)
    fafu 1181 割点
    减肥瘦身健康秘方
    人生的问题
  • 原文地址:https://www.cnblogs.com/tingchen/p/2155662.html
Copyright © 2011-2022 走看看