Custom Configuration 的两种方法:1.Configuration Sections
第一种Configuration Sections
2.CustomConfigurationManager.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) >= ;
- }
- 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) >= )
- {
- 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) >= ;
- }
- 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) >= )
- {
- 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) >= ;
- }
- 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) >= )
- {
- 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
Custom Configuration 的两种方法:1.Configuration Sections的更多相关文章
- Custom Configuration 的两种方法:2.XmlSerializer XmlAttribute
第二种:XmlSerializer XmlAttribute 1.CustomConfiguration.xml 2.CustomConfigurationSetting.cs 3.CustomCon ...
- SpringBoot java配置类@Configuration 的两种写法
首先在Springboot项目中,件一个java类,使用注解@Configuration ,则这个类是SpringBoot bean的创建的配置文件类,,这种配置文件类有两种写法 1.使用包扫描 , ...
- C# web api返回类型设置为json的两种方法
web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...
- C# web api 返回类型设置为json的两种方法
每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...
- Linux安装MySQL的两种方法
转载:http://blog.csdn.net/superchanon/article/details/8546254/ 1. 运行平台:CentOS 6.3 x86_64,基本等同于RH ...
- hive权威安装出现的不解错误!(完美解决)两种方法都可以
以下两种方法都可以,推荐用方法一! 方法一: 步骤一: yum -y install mysql-server 步骤二:service mysqld start 步骤三:mysql -u root - ...
- Cisco设备IOS的恢复方法 两种方法
如果不小心把Router或者Switch的IOS删除了,特别是Flash中的IOS和ROM中的Mini IOS都没有了的话,连启动都不行的话,有什么方法恢复它呢?答案是方法不只一种,而是两种.其实是我 ...
- Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法
Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. fs.listFiles方法,返回Loc ...
- ToStringBuilder学习(二):两种方法用法优缺点及一个问题
研究ApacheCommon源码, 先从一个最简单的开始,即围绕Object类里的toString方法自动化实现的一系列类. 怎么来自动化地实现toString方法, 有两种:反射和手 ...
随机推荐
- vi编辑器中删除文件中所有字符
在命令模式下,将光标移动到文档最上方(使用gg命令),然后输入dG,删除工作区内所有缓存数据. 如果想要删除某行文档以下的内容,将光标移动到文档相应行,然后输入dG即可.
- MMU功能解析、深入剖析、配置与使用
MMU = memory management unit 1.把虚拟地址转化成物理地址,防止地址冲突 2.访问权限管理 MMU把一个虚拟地址的20位到31位作为取出来,建立 一张表,叫做transla ...
- HQL实现模糊查询
hibernate 实现模糊查询两种传参方式,其实各个方法的实质都是一样的,只不过传递参数的方法稍微有点区别 public List<User> getUsers(String id){ ...
- RPM包搭建
打包rpm软件包之spec文件解析 1. 概述 RPM的全称是(Red Hat Package Manager,Red Hat包管理器).RPM是一个开放的软件包管理器,工作在Red Hat.类Lin ...
- 2019 年百度之星·程序设计大赛 - 初赛一 C. HDU 6670 Mindis 离散化+dijkstra
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6670 Mindis Time Limit: 4000/2000 MS (Java/Others) M ...
- ZenCart通过Contact Us接收垃圾邮件的过滤方案
最近收到一些通过Contact Us进行垃圾外链群发的邮件,虽然可以通过在Contact Us增加验证码来解决,但不利于客户体验.所以我们可以通过简单的关键词过滤来实现,一般垃圾外链都含有“[url= ...
- UVA - 1649 Binomial coefficients (组合数+二分)
题意:求使得C(n,k)=m的所有的n,k 根据杨辉三角可以看出,当k固定时,C(n,k)是相对于n递增的:当n固定且k<=n/2时,C(n,k)是相对于k递增的,因此可以枚举其中的一个,然后二 ...
- 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]
题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...
- layui的数据表格加上操作
数据表格加上操作. <script type="text/html" id="barDemo"> <a class="layui-b ...
- pyhton函数
函数编写文档 放在函数开头的字符串称为文档字符串(docstring),将作为函数的一部分存储起来 def square(x): 'Calculates the square of the numbe ...