C#自定义配置文件节的实现
2017-06-07 09:17
148 查看
1.配置文件:(注意configSections必须放在最上面否则会报错)
2.配置文件的主类型也就是配置文件section中type所标记的类(注意应用configuration,dll):
3.子节点类型:
4.后续添加key,value 和多个add节点
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="UserDefinedConfiguration" type="ConsoleApplication9.UserDefinedConfiguration,ConsoleApplication9, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <UserDefinedConfiguration id="12" name="hecong" firstProperty="property2"> <myChildSection url="www.163.com" addr="不知道"></myChildSection><!--子节点--> </UserDefinedConfiguration> </configuration>
2.配置文件的主类型也就是配置文件section中type所标记的类(注意应用configuration,dll):
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication9 { /// <summary> /// 自定义配置文件节点类型 /// </summary> public class UserDefinedConfiguration: ConfigurationSection { private static UserDefinedConfiguration setting; public static UserDefinedConfiguration Setting { get { if (setting == null) setting = (UserDefinedConfiguration)ConfigurationManager.GetSection("UserDefinedConfiguration"); return setting; } } [ConfigurationProperty("id", DefaultValue = "1", IsRequired = true)] [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]//这个属性只是不出现引号中的字符 public long Id { get { return (long)this["id"]; } set { this["id"] = value; } } [ConfigurationProperty("name", DefaultValue = "Lily", IsRequired = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("firstProperty", DefaultValue = "Property1", IsRequired = true)] public string FirstProperty { get { return (string)this["firstProperty"]; } set { this["firstProperty"] = value; } } [ConfigurationProperty("myChildSection")] public UrlConfigurationElement MyChildSection//子节点 { get { return (UrlConfigurationElement)this["myChildSection"]; } set { this["myChildSection"] = value; } } } }
3.子节点类型:
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication9 { public class UrlConfigurationElement: ConfigurationElement { [ConfigurationProperty("url", DefaultValue = "www.baidu.com", IsRequired = true)] public string Url { get { return (string)this["url"]; } set { this["url"] = value; } } [ConfigurationProperty("addr", DefaultValue = "Lily", IsRequired = true)] public string Addr { get { return (string)this["addr"]; } set { this["addr"] = value; } } } }
4.后续添加key,value 和多个add节点
相关文章推荐
- 实现自定义Workflow(2):设置配置文件
- C#读取配置文件详解 完全自定义实现动态加载dll
- Spring Web Flow 简单实现-自定义配置文件位置
- android 自定义ListView 实现 弹出自定义对话框(带EditText)实现 配置文件实现
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
- 利用ScriptableObject在U3D里优雅地实现自定义配置文件
- 实现自定义配置文件加载
- 读取配置文件和自定义配置文件(python实现)
- 一步一步SharePoint 2007之四十三:实现自定义Workflow(2)——设置配置文件
- 通过修改 Apache 的配置文件 htaccess 文件实现自定义404页面
- springboot配置文件中使用配置信息自定义注入哪一个类名(比如多个类实现一个接口,使用resource(name=${配置文件的key}”))
- Log4J日志配置详解和自定义log4j日志级别及输出日志到不同文件实现方法
- .NET 插件系统框架设计(二) 使用对象序列化实现自定义配置文件管理
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
- 读取配置文件和自定义配置文件(python实现)
- 读取配置文件和自定义配置文件(python实现)
- .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点
- Log4J日志配置详解和自定义log4j日志级别及输出日志到不同文件实现方法
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
- 在python中实现自定义配置文件解析及应用技巧[yaml and ConfigParser] 推荐