zoukankan      html  css  js  c++  java
  • 自定义app.config节点(原创) 中庸

         前些天,做过自定义app.config节点的小测试.今天看的时候,把无关的代码去掉,用最少的代码说明问题.以下实例是通过继承ConfigurationSection实现的

     一.效果如下

     <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="CustomSection" type="CustomSectionTest.CLSCustomSection, CustomSectionTest" />
      </configSections>
      <CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />
    </configuration>
    二.代码

        1.自定义类代码方法一

           

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;

    namespace CustomSectionTest
    {
    public sealed class CLSCustomSection : ConfigurationSection
    {

    private ConfigurationPropertyCollection _Properties;

    private readonly ConfigurationProperty _FileName =
    new ConfigurationProperty("fileName",
    typeof(string), "default.txt",
    ConfigurationPropertyOptions.IsRequired);

    private readonly ConfigurationProperty _MaxUsers =
    new ConfigurationProperty("maxUsers",
    typeof(long), (long)1000,
    ConfigurationPropertyOptions.None);

    private readonly ConfigurationProperty _MaxIdleTime =
    new ConfigurationProperty("maxIdleTime",
    typeof(TimeSpan), TimeSpan.FromMinutes(5),
    ConfigurationPropertyOptions.IsRequired);
    public CLSCustomSection()
    {
    _Properties =
    new ConfigurationPropertyCollection();

    _Properties.Add(_FileName);
    _Properties.Add(_MaxUsers);
    _Properties.Add(_MaxIdleTime);
    }


    protected override ConfigurationPropertyCollection Properties
    {
    get
    {
    return _Properties;
    }
    }

    public string FileName
    {
    get
    {
    return (string)this["fileName"];
    }
    set
    {
    this["fileName"] = value;
    }
    }

    public long MaxUsers
    {
    get
    {
    return (long)this["maxUsers"];
    }
    set
    {
    this["maxUsers"] = value;
    }
    }

    public TimeSpan MaxIdleTime
    {
    get
    {
    return (TimeSpan)this["maxIdleTime"];
    }
    set
    {
    this["maxIdleTime"] = value;
    }
    }
    }
    }

             自定义类方法二

                 

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;

    namespace CustomSectionTest
    {
    public sealed class CLSCustomSection : ConfigurationSection
    {
    [ConfigurationProperty("fileName",
    DefaultValue = "default.txt",
    IsRequired = true)]
    public string FileName
    {
    get
    {
    return (string)this["fileName"];
    }
    set
    {
    this["fileName"] = value;
    }
    }
    [ConfigurationProperty("maxUsers",
    DefaultValue = "1000",
    IsRequired = false)]
    public long MaxUsers
    {
    get
    {
    return (long)this["maxUsers"];
    }
    set
    {
    this["maxUsers"] = value;
    }
    }

    [ConfigurationProperty("maxIdleTime",
    DefaultValue = "00:15:00",
    IsRequired = false)]
    public TimeSpan MaxIdleTime
    {
    get
    {
    return (TimeSpan)this["maxIdleTime"];
    }
    set
    {
    this["maxIdleTime"] = value;
    }
    }
    }
    }

        2.调用测试

         CLSCustomSection customsection =(CLSCustomSection)ConfigurationManager.GetSection("CustomSection");
         MessageBox.Show(customsection.FileName);

  • 相关阅读:
    Quartus II 12.0傻瓜版安装教程(附下载链接)
    MathType 免费版安装教程(附下载链接)
    Matlab与Visual Studio 安装和联合编译配置(附下载链接)
    操作系统的概述
    Sublime Text 3 安装,C/C++代码的编译运行以及代码格式化的配置 (附下载链接)
    DSP中的DELAY_US ( )函数存放在FLASH下执行在RAM中的方法
    vtk:如何利用鼠标交会三维模型,获取交点坐标?
    进一步完善了近景摄影测量和低空数据处理软件!
    SURF算法速度更快!
    vtk:如何使用鼠标选中多个点?
  • 原文地址:https://www.cnblogs.com/liangjie/p/2200704.html
Copyright © 2011-2022 走看看