Custom app. config node (original)

Source: Internet
Author: User

I did a small test on the custom app. config node a few days ago. When I read it today, I removed irrelevant code and described the problem with the least amount of code. The following instances are implemented by inheriting the ConfigurationSection.

I. The results are as follows:

<? 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>
Ii. Code

1. Custom class code method 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;
}
}
}
}

 

Custom method 2

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. Call the test

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.