using
System;
using
System.Collections;
using
System.Text;
using
System.Configuration;
namespace MyConfigSectionHandler
{
public
sealed
class
MyHandler
: ConfigurationSection
{
// 文字列
[ConfigurationProperty("fileName",
DefaultValue =
"Test.html", IsRequired =
true)]
[StringValidator(InvalidCharacters
= "
~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public
string
FileName
{
get
{
return
(string)this["fileName"];
}
set
{
this["fileName"]
= value;
}
}
// 数値
[ConfigurationProperty("maxUsers",
DefaultValue = 10, IsRequired =
false)]
[IntegerValidator(MinValue
= 1, MaxValue = 100, ExcludeRange =
false)]
public
int
MaxUsers
{
get
{
return
(int)this["maxUsers"];
}
set
{
this["maxUsers"]
= value;
}
}
// 時間間隔
[ConfigurationProperty("maxIdleTime")]
[TimeSpanValidator(MinValueString
= "0:0:0",
MaxValueString =
"1:23:45", ExcludeRange =
false)]
public
TimeSpan
MaxIdleTime
{
get
{
return
(TimeSpan)this["maxIdleTime"];
}
set
{
this["maxIdleTime"]
= value;
}
}
// 子要素
[ConfigurationProperty("myChildSection")]
public
ChildConfigElement
MyChildSection
{
get
{
return
(ChildConfigElement)this["myChildSection"];
}
set
{
this["myChildSection"]
= value;
}
}
}
public
class
ChildConfigElement
: ConfigurationElement
{
public
ChildConfigElement()
{}
public
ChildConfigElement(String
a1, String
a2)
{
MyChildAttribute1 = a1;
MyChildAttribute2 = a2;
}
[ConfigurationProperty("myChildAttrib1",
DefaultValue = "Hello",
IsRequired =
true)]
[StringValidator(InvalidCharacters
=
"~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public
String
MyChildAttribute1
{
get
{
return
(String)this["myChildAttrib1"];
}
set
{
this["myChildAttrib1"]
= value;
}
}
[ConfigurationProperty("myChildAttrib2",
DefaultValue = "World",
IsRequired =
true)]
[StringValidator(InvalidCharacters
=
"~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public
String
MyChildAttribute2
{
get
{
return
(String)this["myChildAttrib2"];
}
set
{
this["myChildAttrib2"]
= value;
}
}
}
} |