トップ 履歴 一覧 カテゴリ ソース 検索 ヘルプ RSS ログイン

DotNet/CustomConfiguration

INDEX

カスタム構成要素

構成ファイル(app.config 等)内に、独自の設定内容を作成する。

抽象クラス System.Configuration.ConfigurationSection などを実装し、構成ファイルの configSections 要素で作成したクラスを指定する。

 サンプルコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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