カスタム構成要素
構成ファイル(app.config 等)内に、独自の設定内容を作成する。
抽象クラス System.Configuration.ConfigurationSection などを実装し、構成ファイルの configSections 要素で作成したクラスを指定する。
サンプルコード
1 |
namespace ScrapCode.DotNet.Configuration
{
/// <summary>
/// 構成ファイル内のカスタムセクションを表します。
/// </summary>
public class CustomSection : System.Configuration.ConfigurationSection
{
[System.Configuration.ConfigurationProperty("stringValue", IsRequired = true)]
[System.Configuration.StringValidator(MinLength = 0, MaxLength = 64, InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\")]
public string StringValue
{
get { return (string)this["stringValue"]; }
set { this["stringValue"] = value; }
}
[System.Configuration.ConfigurationProperty("integerValue", IsRequired = true)]
[System.Configuration.LongValidator(MinValue = 0, MaxValue = 123456789, ExcludeRange = false)]
public long IntegerValue
{
get { return (long)this["integerValue"]; }
set { this["integerValue"] = value; }
}
[System.Configuration.ConfigurationProperty("timeSpanValue", DefaultValue = "0:10:00")]
[System.Configuration.TimeSpanValidator(MinValueString = "0:0:00", MaxValueString = "1:0:0", ExcludeRange = false)]
public System.TimeSpan TimeSpanValue
{
get { return (System.TimeSpan)this["timeSpanValue"]; }
set { this["timeSpanValue"] = value; }
}
[System.Configuration.ConfigurationProperty("urls")]
[System.Configuration.ConfigurationCollection(typeof(CustomCollection))]
public CustomCollection Urls
{
get { return (CustomCollection)this["urls"]; }
}
}
/// <summary>
/// 子要素のコレクションを格納するカスタム構成要素を表します。
/// </summary>
public class CustomCollection : System.Configuration.ConfigurationElementCollection
{
public CustomElement this[int index]
{
get { return (CustomElement)BaseGet(index); }
set { if (BaseGet(index) != null) BaseRemoveAt(index); BaseAdd(index, value); }
}
public new CustomElement this[string key]
{
get { return (CustomElement)BaseGet(key); }
set { if (BaseGet(key) != null) BaseRemoveAt(BaseIndexOf(BaseGet(key))); BaseAdd(value); }
}
protected override System.Configuration.ConfigurationElement CreateNewElement()
{
return new CustomElement();
}
protected override object GetElementKey(System.Configuration.ConfigurationElement element)
{
return ((CustomElement)element).Name;
}
}
/// <summary>
/// 構成ファイル内のカスタム構成要素を表します。
/// </summary>
public class CustomElement : System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string)base["name"]; }
set { base["name"] = value; }
}
[System.Configuration.ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get { return (string)base["url"]; }
set { base["url"] = value; }
}
[System.Configuration.ConfigurationProperty("port", DefaultValue = (int)443, IsRequired = false)]
[System.Configuration.IntegerValidator(MinValue = 0, MaxValue = 65535, ExcludeRange = false)]
public int Port
{
get { return (int)base["port"]; }
set { base["port"] = value; }
}
}
}
/* ** 構成ファイルの記載例 **
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="CustomSection" type="ScrapCode.DotNet.Configuration.CustomSection, ConsoleApplication" />
</configSections>
<CustomSection stringValue="default.txt" integerValue="1000" timeSpanValue="00:15:00">
<urls>
<add name="sample" url="https://example.com/" port="443" />
</urls>
</CustomSection>
</configuration>
*/
/* ** アプリケーションからの参照例 **
class Program
{
static void Main(string[] args)
{
ScrapCode.DotNet.Configuration.CustomSection config =
(ScrapCode.DotNet.Configuration.CustomSection)ConfigurationManager.GetSection("CustomSection");
System.Console.Out.Write("stringValue=");System.Console.Out.WriteLine(config.StringValue);
System.Console.Out.Write("integerValue=");System.Console.Out.WriteLine(config.IntegerValue);
System.Console.Out.Write("timeSpanValue=");System.Console.Out.WriteLine(config.TimeSpanValue);
foreach (ScrapCode.DotNet.Configuration.CustomElement url in config.Urls)
{
System.Console.Out.Write("- name="); System.Console.Out.Write(url.Name);
System.Console.Out.Write(", url="); System.Console.Out.Write(url.Url);
System.Console.Out.Write(", port="); System.Console.Out.Write(url.Port);
System.Console.Out.WriteLine();
}
}
}
*/ |
構成ファイルの記載例
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="CustomSection" type="ScrapCode.DotNet.Configuration.CustomSection, ConsoleApplication" />
</configSections>
<CustomSection stringValue="default.txt" integerValue="1000" timeSpanValue="00:15:00">
<urls>
<add name="sample" url="https://example.com/" port="443" />
</urls>
</CustomSection>
</configuration>
アプリケーションからの参照例
class Program
{
static void Main(string[] args)
{
ScrapCode.DotNet.Configuration.CustomSection config =
(ScrapCode.DotNet.Configuration.CustomSection)ConfigurationManager.GetSection("CustomSection");
System.Console.Out.Write("stringValue=");System.Console.Out.WriteLine(config.StringValue);
System.Console.Out.Write("integerValue=");System.Console.Out.WriteLine(config.IntegerValue);
System.Console.Out.Write("timeSpanValue=");System.Console.Out.WriteLine(config.TimeSpanValue);
foreach (ScrapCode.DotNet.Configuration.CustomElement url in config.Urls)
{
System.Console.Out.Write("- name="); System.Console.Out.Write(url.Name);
System.Console.Out.Write(", url="); System.Console.Out.Write(url.Url);
System.Console.Out.Write(", port="); System.Console.Out.Write(url.Port);
System.Console.Out.WriteLine();
}
}
}
最終更新時間:2026年03月08日 18時47分03秒 指摘や意見などあればSandBoxのBBSへ。
CustomConfiguration.cs