.NET カスタム構成セクションを作成する

方法 : ConfigurationSection を使用してカスタム構成セクションを作成する


アプリケーション構成ファイルに以下のようなカスタム構成セクションを定義し読み込みたいと思います。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<section name="customConfigSection"  type="CustomConfigSample.CustomConfig.CustomConfigSection, CustomConfigSample" />
</configSections>


<customConfigSection>
<childElemnts>
<add key="key1" value1="Value1_1" value2="Value2_1" />
<add key="key2" value1="Value1_2" value2="Value2_2" />
<add key="key3" value1="Value1_3"  />
</childElemnts >
</customConfigSection >

</system.diagnostics>
</configuration>


1、Windowsアプリケーション「CustomConfigSample」を作成します。

2、参照設定でSystem.configurationの参照を追加します。

3、カスタム構成セクションクラスを作成します。
Imports System
Imports System.Collections
Imports System.Text
Imports System.Configuration
Imports System.Xml

Namespace CustomConfig

''' <summary>
''' カスタムセクション
''' </summary>
''' <remarks></remarks>
Public Class CustomConfigSection
Inherits ConfigurationSection

''' <summary>
''' コンストラクタ
''' </summary>
''' <remarks></remarks>
Public Sub New()
End Sub

''' <summary>
''' 子要素を表すコレクションです。
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<ConfigurationProperty("childElemnts")> _
Public Property ChildElemnts() As CustomConfigElementCollection
Get
Return CType(Me("childElemnts"), CustomConfigElementCollection)
End Get
Set(ByVal value As CustomConfigElementCollection)
Me("childElemnts") = CType(value, CustomConfigElementCollection)
End Set
End Property
End Class


End Namespace


3、カスタム構成セクションの子要素クラスを作成します。
Imports System
Imports System.Collections
Imports System.Text
Imports System.Configuration
Imports System.Xml

Namespace CustomConfig

''' <summary>
''' カスタムセクション子要素を定義するクラスです。
''' </summary>
''' <remarks></remarks>
Public Class CustomConfigElement
Inherits ConfigurationElement

''' <summary>
''' コンストラクタ
''' </summary>
''' <remarks></remarks>
Public Sub New()
End Sub

''' <summary>
''' キー
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<ConfigurationProperty("key", IsRequired:=True)> _
Public Property Key() As String
Get
Return CStr(Me("key"))
End Get
Set(ByVal value As String)
Me("key") = value
End Set
End Property

''' <summary>
''' 値1
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<ConfigurationProperty("value1", IsRequired:=True)> _
Public Property Value1() As String
Get
Return CStr(Me("value1"))
End Get
Set(ByVal value As String)
Me("value1") = value
End Set
End Property

''' <summary>
''' 値2
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<ConfigurationProperty("value2", IsRequired:=False)> _
Public Property Value2() As String
Get
Return CStr(Me("value2"))
End Get
Set(ByVal value As String)
Me("value2") = value
End Set
End Property

End Class
End Namespace


4、カスタム構成セクションの子要素のコレクションクラスを作成します。
Imports System
Imports System.Collections
Imports System.Text
Imports System.Configuration
Imports System.Xml

Namespace CustomConfig

''' <summary>
''' CustomConfigElementのコレクションを扱うためのクラスです。
''' </summary>
''' <remarks></remarks>
Public Class CustomConfigElementCollection
Inherits ConfigurationElementCollection

''' <summary>
''' コンストラクタ
''' </summary>
''' <remarks></remarks>
Public Sub New()
End Sub

''' <summary>
''' 指定したインデックスのCustomConfigElementを取得します。
''' </summary>
''' <param name="index"></param>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Default Public Shadows Property Item(ByVal index As Integer) As CustomConfigElement
Get
Return CType(BaseGet(index), CustomConfigElement)
End Get
Set(ByVal value As CustomConfigElement)
If Not (BaseGet(index) Is Nothing) Then
BaseRemoveAt(index)
End If
BaseAdd(index, value)
End Set
End Property

''' <summary>
''' 指定した名称のCustomConfigElementを取得します。
''' </summary>
''' <param name="Name"></param>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Default Public Shadows ReadOnly Property Item(ByVal Name As String) As CustomConfigElement
Get
Return CType(BaseGet(Name), CustomConfigElement)
End Get
End Property

''' <summary>
''' 新しい ConfigurationElement を作成します。  
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
Return New CustomConfigElement()
End Function

''' <summary>
''' 指定したConfigurationElementを特定するキーを取得します。  
''' </summary>
''' <param name="element"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As Object
Return CType(element, CustomConfigElement).Key
End Function

End Class
End Namespace


5、アプリケーション構成ファイルのカスタムセクションをアプリケーションから読み込みます。
Public Class Main


Public Shared Sub Main()

Dim section As CustomConfig.CustomConfigSection
section = CType(System.Configuration.ConfigurationManager.GetSection("customConfigSection"), CustomConfig.CustomConfigSection)

Console.WriteLine("customConfigSectionの要素数={0}", section.ChildElemnts.Count)

Console.WriteLine("customConfigSectionの0番目要素のKey={0}", section.ChildElemnts.Item(0).Key)

Console.WriteLine("customConfigSectionの[key2]要素のValue1={0}", section.ChildElemnts.Item("key2").Value1)

End Sub

End Class