zoukankan      html  css  js  c++  java
  • C#自定义配置文件节的实现

    1.配置文件:(注意configSections必须放在最上面否则会报错)

    <?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节点

  • 相关阅读:
    解决:信息中插入avi格式的视频时,提示“unsupported video format”
    java字节数组和16进制之间的转换
    16进制转换字节数组工具类
    如何在ubuntu 12.04 中安装经典的 GNOME桌面
    Ubuntu安装软件提示”需要安装不能信任的软件包”解决办法
    Ubuntu系统下运行Eclipse出现找不到jre的问题的解决方法
    ubuntu添加共享出错
    从scrapy使用经历说开来
    有趣的问题--12 coins problem
    一个奇怪的MySQL错误返回
  • 原文地址:https://www.cnblogs.com/rengke2002/p/6955286.html
Copyright © 2011-2022 走看看