namespace ScrapCode.DotNet.Configuration { /// /// 構成ファイル内のカスタムセクションを表します。 /// 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"]; } } } /// /// 子要素のコレクションを格納するカスタム構成要素を表します。 /// 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; } } /// /// 構成ファイル内のカスタム構成要素を表します。 /// 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; } } } } /* ** 構成ファイルの記載例 **
*/ /* ** アプリケーションからの参照例 ** 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(); } } } */