zoukankan      html  css  js  c++  java
  • Custom Configuration 的两种方法:1.Configuration Sections

    第一种Configuration Sections

    1.App.config

    2.CustomConfigurationManager.cs

    3.TestProgram.cs.

    App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <section name="CustomSettings" type="CustomConfigurationSection.CustomSettingsConfigurationSection,CustomConfigurationSection"/>
        </configSections>
        <appSettings></appSettings>
        <CustomSettings>
            <SElement name="name01" value="value01" />
            <MElement name="mElement">
                <SElement01 name="name02" value="value02" />
                <SElement02 name="name03" value="value03" />
            </MElement>
            <CElement>
                <add key="key01" value="value04" />
                <add key="key02" value="value05" />
            </CElement>
            <NElement>
                <add key="key03" value="value06">
                    <SElement03 name="name04" value="value07" />
                </add>
                <add key="key04" value="value08">
                    <SElement03 name="name05" value="value09" />
                </add>
            </NElement>
            <M2Element>
                <SElement name="name06" value="value10" />
                <SElement name="name07" value="value11" />
            </M2Element>
      </CustomSettings>
    </configuration>

    CustomConfigurationManager.cs

    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;
    using System.Text;
    using System.IO;
    using System.Configuration;
    using System.Xml;
    using System.Xml.XPath;
    using System.Xml.Serialization;
    
    namespace CustomConfigurationSection
    {
        public class CustomSettingsConfigurationSection : ConfigurationSection
        {
            [ConfigurationProperty("SElement")]
            public SElement SElement
            {
                get { return (SElement)this["SElement"]; }
            }
    
            [ConfigurationProperty("MElement")]
            public MElement MElement
            {
                get { return (MElement)this["MElement"]; }
            }
    
            [ConfigurationProperty("CElement")]
            public TElementCollection CElement
            {
                get { return (TElementCollection)this["CElement"]; }
            }
    
            [ConfigurationProperty("NElement")]
            public NElementCollection NElement
            {
                get { return (NElementCollection)this["NElement"]; }
            }
    
            [ConfigurationProperty("M2Element")]
            [ConfigurationCollection(typeof(SElement),AddItemName="SElement")]
            public SElementCollection M2Element
            {
                get { return (SElementCollection)this["M2Element"]; }
            }
        }
    
        public class SElement : ConfigurationElement
        {
            [ConfigurationProperty("name")]
            public String Name
            {
                get
                {
                    return (String)this["name"];
                }
                set
                {
                    this["name"] = value;
                }
            }
            [ConfigurationProperty("value")]
            public String Value
            {
                get
                {
                    return (String)this["value"];
                }
                set
                {
                    this["value"] = value;
                }
            }
        }
    
        public class MElement : ConfigurationElement
        {
            [ConfigurationProperty("name")]
            public string Name
            {
                get { return (string)this["name"]; }
            }
    
            [ConfigurationProperty("SElement01")]
            public SElement SElement01
            {
                get { return (SElement)this["SElement01"]; }
            }
            [ConfigurationProperty("SElement02")]
            public SElement SElement02
            {
                get { return (SElement)this["SElement02"]; }
            }
        }
        
        public class TElement : ConfigurationElement
        {
            [ConfigurationProperty("key")]
            public String Key
            {
                get
                {
                    return (String)this["key"];
                }
                set
                {
                    this["key"] = value;
                }
            }
    
            [ConfigurationProperty("value")]
            public String Value
            {
                get
                {
                    return (String)this["value"];
                }
                set
                {
                    this["value"] = value;
                }
            }
        }
    
        public class NElement : ConfigurationElement
        {
            [ConfigurationProperty("key")]
            public String Key
            {
                get
                {
                    return (String)this["key"];
                }
                set
                {
                    this["key"] = value;
                }
            }
    
            [ConfigurationProperty("value")]
            public String Value
            {
                get
                {
                    return (String)this["value"];
                }
                set
                {
                    this["value"] = value;
                }
            }
    
            [ConfigurationProperty("SElement03")]
            public SElement NestedElement
            {
                get { return (SElement)this["SElement03"]; }
            }
        }
    
        public class TElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new TElement();
            }
    
            protected override object GetElementKey(ConfigurationElement item)
            {
                TElement element = (TElement)item;
                return getKey(element);
            }
    
            public TElement this[int index]
            {
                get
                {
                    return (TElement)BaseGet(index);
                }
                set
                {
                    if (BaseGet(index) != null)
                    {
                        BaseRemove(index);
                    }
                    BaseAdd(index, value);
                }
            }
    
            public new TElement this[string name]
            {
                get
                {
                    return (TElement)BaseGet(name);
                }
            }
    
            public new int Count
            {
                get { return base.Count; }
            }
    
            public int IndexOf(TElement item)
            {
                return BaseIndexOf(item);
            }
    
            public void RemoveAt(int index)
            {
                BaseRemoveAt(index);
            }
    
            public void Add(TElement item)
            {
                BaseAdd(item);
            }
    
            public void Clear()
            {
                BaseClear();
            }
    
            public bool Contains(TElement item)
            {
                return BaseIndexOf(item) >= 0;
            }
    
            public void CopyTo(TElement[] array, int arrayIndex)
            {
                base.CopyTo(array, arrayIndex);
            }
    
            public new bool IsReadOnly
            {
                get { return false; }
            }
    
            public bool Remove(TElement item)
            {
                if (BaseIndexOf(item) >= 0)
                {
                    BaseRemove(item);
                    return true;
                }
                return false;
            }
    
            private string getKey(TElement item)
            {
                return item.Key;
            }
        }
    
        public class NElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new NElement();
            }
    
            protected override object GetElementKey(ConfigurationElement item)
            {
                NElement element = (NElement)item;
                return getKey(element);
            }
    
            public NElement this[int index]
            {
                get
                {
                    return (NElement)BaseGet(index);
                }
                set
                {
                    if (BaseGet(index) != null)
                    {
                        BaseRemove(index);
                    }
                    BaseAdd(index, value);
                }
            }
    
            public new NElement this[string name]
            {
                get
                {
                    return (NElement)BaseGet(name);
                }
            }
    
            public new int Count
            {
                get { return base.Count; }
            }
    
            public int IndexOf(NElement item)
            {
                return BaseIndexOf(item);
            }
    
            public void RemoveAt(int index)
            {
                BaseRemoveAt(index);
            }
    
            public void Add(NElement item)
            {
                BaseAdd(item);
            }
    
            public void Clear()
            {
                BaseClear();
            }
    
            public bool Contains(NElement item)
            {
                return BaseIndexOf(item) >= 0;
            }
    
            public void CopyTo(NElement[] array, int arrayIndex)
            {
                base.CopyTo(array, arrayIndex);
            }
    
            public new bool IsReadOnly
            {
                get { return false; }
            }
    
            public bool Remove(NElement item)
            {
                if (BaseIndexOf(item) >= 0)
                {
                    BaseRemove(item);
                    return true;
                }
                return false;
            }
    
            private string getKey(NElement item)
            {
                return item.Key;
            }
        }
    
        public class SElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new SElement();
            }
    
            protected override object GetElementKey(ConfigurationElement item)
            {
                SElement element = (SElement)item;
                return getKey(element);
            }
    
            public SElement this[int index]
            {
                get
                {
                    return (SElement)BaseGet(index);
                }
                set
                {
                    if (BaseGet(index) != null)
                    {
                        BaseRemove(index);
                    }
                    BaseAdd(index, value);
                }
            }
    
            public new SElement this[string name]
            {
                get
                {
                    return (SElement)BaseGet(name);
                }
            }
    
            public new int Count
            {
                get { return base.Count; }
            }
    
            public int IndexOf(SElement item)
            {
                return BaseIndexOf(item);
            }
    
            public void RemoveAt(int index)
            {
                BaseRemoveAt(index);
            }
    
            public void Add(SElement item)
            {
                BaseAdd(item);
            }
    
            public void Clear()
            {
                BaseClear();
            }
    
            public bool Contains(SElement item)
            {
                return BaseIndexOf(item) >= 0;
            }
    
            public void CopyTo(SElement[] array, int arrayIndex)
            {
                base.CopyTo(array, arrayIndex);
            }
    
            public new bool IsReadOnly
            {
                get { return false; }
            }
    
            public bool Remove(SElement item)
            {
                if (BaseIndexOf(item) >= 0)
                {
                    BaseRemove(item);
                    return true;
                }
                return false;
            }
    
            private string getKey(SElement item)
            {
                return item.Name;
            }
        }
    
    }

    TestProgram.cs

    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using System.Configuration;
    
    namespace CustomConfigurationSection
    {
        public class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    CustomSettingsConfigurationSection config = (CustomSettingsConfigurationSection)System.Configuration.ConfigurationManager.GetSection("CustomSettings");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }
        }
    }

    http://blog.csdn.net/zpx3243/article/details/5970585

  • 相关阅读:
    what's the 头寸
    pandas操作速查表
    基于TensorFlow的简单验证码识别
    Python识别字符型图片验证码
    使用python以及工具包进行简单的验证码识别
    Mac OS X中MacPorts安装和使用
    通过Tesseract实现简单的OCR
    报错解决——pytesseract.pytesseract.TesseractError: (1,’Error opening data file /usr/local/share/tessdata/eng.traineddata’)
    各版本系统安装tesseract-ocr
    报错解决——-bash: wget: command not found
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/7146909.html
Copyright © 2011-2022 走看看