zoukankan      html  css  js  c++  java
  • .NET 配置项扩展

    using System;
    using System.Configuration;
    
    namespace ConsoleApplication3
    {
        /*
            web.config 或 app.config 中的配置示例如下(注:configSections 节一定要是 configuration 节下的第一个元素):
         
            <?xml version="1.0" encoding="utf-8" ?>
            <configuration>
                <configSections>
                    <section name="urls" type="ConsoleApplication3.EasConfigurationSection, ConsoleApplication3"/>
                </configSections>
        
                <urls>
                    <url name="cn.bing.com" url="http://cn.bing.com/" port="80"></url>
                </urls>
            </configuration>
         */
    
        public class EasConfigurationElement : ConfigurationElement
        {
            [ConfigurationProperty("name", IsKey=true, IsRequired=true)]
            public string Name
            {
                get { return (string)this["name"]; }
                set { this["name"] = value; }
            }
    
            [ConfigurationProperty("url", DefaultValue = "")]
            public string Url
            {
                get { return (string)this["url"]; }
                set { this["url"] = value; }
            }
    
            [ConfigurationProperty("port", DefaultValue = 0, IsRequired = false)]
            [IntegerValidator(MinValue = 0, MaxValue = 8080, ExcludeRange = false)]
            public int Port
            {
                get { return (int)this["port"]; }
                set { this["port"] = value; }
            }
        }
    
        [ConfigurationCollection(typeof(EasConfigurationElement))]
        public class EasConfigurationElementCollection : ConfigurationElementCollection
        {
            protected override string ElementName
            {
                get
                {
                    return "url";
                }
            }
    
            public override ConfigurationElementCollectionType CollectionType
            {
                get { return ConfigurationElementCollectionType.BasicMap; }
            }
    
            protected override ConfigurationElement CreateNewElement()
            {
                return new EasConfigurationElement();
            }
    
            protected override object GetElementKey( ConfigurationElement element )
            {
                var newElement = element as EasConfigurationElement;
    
                if( newElement != null ) return newElement.Name;
    
                throw new NullReferenceException( "element can not convert to " + typeof( EasConfigurationElement ) );
            }
    
            public EasConfigurationElement this[ int index ]
            {
                get { return (EasConfigurationElement)BaseGet( index ); }
                set
                {
                    if( BaseGet( index ) != null )
                    {
                        BaseRemoveAt( index );
                    }
    
                    BaseAdd( index, value );
                }
            }
        }
    
        public class EasConfigurationSection : ConfigurationSection
        {
            [ConfigurationProperty("", IsDefaultCollection = true)]
            public EasConfigurationElementCollection Urls
            {
                get
                {
                    var collection = (EasConfigurationElementCollection)base[""];
    
                    return collection;
                }
            }
        }
    }
    本博客内容,如需转载请务必保留超链接。

    Contact Me:Mail此处省略好几个字...
  • 相关阅读:
    监听浏览器使用不同版本js并且处理ie兼容getElementByClassName
    超出字数部分省略(主要解决不兼容;display: -webkit-box;的浏览器)
    highcharts图表
    整理前端css/js/jq常见问题及解决方法(1)
    [转载]移动页面所需meta元素和Viewport窗口知识点
    在ie和chrome浏览器中滚动条样式的设置
    关于:before :after
    mysql备份与还原
    使用xkbeancomparator对比javabean,生成操作记录
    SSO单点登录和CAS
  • 原文地址:https://www.cnblogs.com/jroger/p/5029030.html
Copyright © 2011-2022 走看看